Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Query about retrieving values from a drop down menu
Old 09-24-2008, 11:11 AM Query about retrieving values from a drop down menu
Novice Talker

Posts: 13
Location: UK
Trades: 0
Hi,

A quick question, but when you create a dropdown box, is an array created automatically to hold the possible values that are stored in it, and if I was to insert those selected values from a dropdown menu into a database, what would the code look like if I needed to get the current value that was selected?
patelh5 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-24-2008, 11:24 AM Re: Query about retrieving values from a drop down menu
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
If by a dropdown box, you mean a select list in a form, the answer is, that you may CREATE an array from the DOM by looping over the list of <option> values in JavaScript, then pass this information to a PHP script via an AJAX call.

To get the selected value, however, you would do something like this: http://www.quirksmode.org/js/forms.html#sselect
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-24-2008, 11:39 AM Re: Query about retrieving values from a drop down menu
Novice Talker

Posts: 13
Location: UK
Trades: 0
Is there not an easier way, I don't know javascript at all, and that link doesnt seem clear as to what it will do?
patelh5 is offline
Reply With Quote
View Public Profile
 
Old 09-24-2008, 12:02 PM Re: Query about retrieving values from a drop down menu
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Only JavaScript has access to the DOM, and may create arrays from it. PHP is done working by the time the HTML is delivered to the browser. There may be other ways you could invent to handle this, like storing the select values manually on the server side, then looping though and inserting them one at a time, but PHP can't read what is in the browser.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 09-24-2008 at 12:03 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-24-2008, 12:37 PM Array and drop down boxes
Novice Talker

Posts: 13
Location: UK
Trades: 0
I need to create an array to populate the options in my dropdown menu as such:-
PHP Code:
Choose the Type of Issue
<select name="dropdown" Id="dropdown">
  <
option value="General">General</option>
  <
option value="Server">Server</option>
  <
option value="Database">Database</option>
  <
option value="Environment">Environment</option>
  <
option value="Requirement">Requirement</option


I was wondering whether this would be appropriate so I can take the value selected and throw that in the database with the 'post' function? where obviously, 'dropdown' will be the name of the form.

<FONT face="Courier New">
PHP Code:
<?php
$a 
= array ('a' => 'General''b' => 'Server''c' => array ('x''y''z'));
?>
patelh5 is offline
Reply With Quote
View Public Profile
 
Old 09-24-2008, 12:38 PM Re: Query about retrieving values from a drop down menu
Novice Talker

Posts: 13
Location: UK
Trades: 0
Ok, what is DOM?
patelh5 is offline
Reply With Quote
View Public Profile
 
Old 09-24-2008, 12:48 PM Re: Query about retrieving values from a drop down menu
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
[threads on same topic merged]
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-24-2008, 12:48 PM Re: Query about retrieving values from a drop down menu
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://www.google.com/search?q=DOM
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-24-2008, 12:52 PM Re: Query about retrieving values from a drop down menu
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
DOM: Document Object Model. This is basically how JavaScript reads what is in the browser. The DOM includes not only what is in the source code, but anything that has been inserted dynamically on the client side. It will not include anything that has been removed. The DOM is also a structure of relationships between the various objects that make up what is being stored by the browser. These relationships are child/parent/sibling. Along with these, are the various attributes that any element could have, such as what is stored inside of a value="", or id="" attribute.

That is a simple overview. To learn more about the DOM go here: http://www.w3.org/DOM/
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-24-2008, 01:01 PM Re: Query about retrieving values from a drop down menu
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
As far as inserting just the selected value, you could, on your script page (that is targeted from the form), access it via the $_POST["dropdown"] variable, using the example you gave above.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-24-2008, 01:03 PM Re: Query about retrieving values from a drop down menu
Novice Talker

Posts: 13
Location: UK
Trades: 0
Ok, I am just thinking, that I have multiple check boxes, as I have had a change in design in mind. I cannot use drop down boxes because the user may have more than one issue in mind to discuss in one text area, so multiple check boxes would be appropriate, how would that work?
patelh5 is offline
Reply With Quote
View Public Profile
 
Old 09-24-2008, 01:05 PM Re: Query about retrieving values from a drop down menu
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Maybe you should take a quick tutorial about forms and PHP: http://www.w3schools.com/php/php_forms.asp
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-24-2008, 01:07 PM Re: Query about retrieving values from a drop down menu
Novice Talker

Posts: 13
Location: UK
Trades: 0
I have actually read that fully, my textarea inserts all data into the database fine and I used that exact tutorial to assist me. I mean how it would work with multiple check boxes chosen or one etc, I am guessing in the same way as a drop down box, or perhaps just one at a time u take the value from one checkbox and insert into the appropriate db column, like I already have with my single checkbox.....
patelh5 is offline
Reply With Quote
View Public Profile
 
Old 09-24-2008, 01:47 PM Re: Query about retrieving values from a drop down menu
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
My thoughts - your not understanding the basics.

You need to spend a few hours and study php.net, w3schools, download an ebook or two, go the library etc.
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 09-24-2008, 05:02 PM Re: Query about retrieving values from a drop down menu
Novice Talker

Posts: 13
Location: UK
Trades: 0
Ok spent a few hours reading the basics once again from the w3schools site, I read up on arrays mainly too.

With one piece of data to be sent, you can just store that into a variable and call that in post and insert into db fine.

But with multiple selections of data from checkboxes, you would obviously have to create an array first, which is stored in a variable($dropdown) that is then called using the post function(im guessing) and inserted into the relevant table column of that table in the database. With the array function, I've never really written one and looking at some manuals and tutorials, I was wondering whether this would be a correct way of writing one first hand:- associative array:-

PHP Code:
$ages = array("general"=>value1"server"=>value2); 
This is the checkbox selection:-

PHP Code:
Choose the Type of Issue
<input type="checkbox" name="general" value="value1">General
<input type="checkbox" name="server" value="value2">Server 
patelh5 is offline
Reply With Quote
View Public Profile
 
Old 09-24-2008, 05:36 PM Re: Query about retrieving values from a drop down menu
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
first off your "choices" should be radio buttons so only one can be selected.

secondly you are misunderstanding HTTP POST/GET requests, an unchecked checkbox will return nothing, there will only be a key/value pair returned in the POST collection IF the box is checked.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-24-2008, 05:46 PM Re: Query about retrieving values from a drop down menu
Novice Talker

Posts: 13
Location: UK
Trades: 0
yes thats fine. but the user may want to pick two options they have an issue with when raising their request in the system - sorry probably didnt make that one clear, so perhaps not radio buttons?

And yeah an unchecked box should return nothing, thats fine because nothing will be in the database if it's not selected, but I need a string to be present in the database if it is selected:-

PHP Code:
$general=$_POST['general']; 
PHP Code:
if ($general != NULL) { 
$general "general"
this will store the string if selection is made, but how about multiple selections? or other options that are available, 'general' obviously isnt the only option available at the moment:-

Choose the Type of Issue
<input type="checkbox" name="general" value="value1">General
<input type="checkbox" name="server" value="value2">Server


just need help on how this can be constructed, could be related to using an array etc....I have seen javascript can be used, but not too familiar with it...
patelh5 is offline
Reply With Quote
View Public Profile
 
Old 09-24-2008, 06:01 PM Re: Query about retrieving values from a drop down menu
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
make HTTP work for you

Form elements with the same name will be passed in the POST collection as a comma separated string value.

so give all the checkboxes a name of "issue" say, then $_POST["issue"] will have the values from the checked items only as a CSV ie "value1,value3,value5"

you then explode() the string using "," as the delimiter.

The same happens with multiple selections from a "dropdown". The selected options are returned as a CSV so can be treated in the same way.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Query about retrieving values from a drop down menu
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.00978 seconds with 12 queries