Query about retrieving values from a drop down menu
09-24-2008, 11:11 AM
|
Query about retrieving values from a drop down menu
|
Posts: 13
Location: UK
|
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?
|
|
|
|
09-24-2008, 11:24 AM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
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
|
|
|
|
09-24-2008, 11:39 AM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 13
Location: UK
|
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? 
|
|
|
|
09-24-2008, 12:02 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
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.
Last edited by wayfarer07; 09-24-2008 at 12:03 PM..
|
|
|
|
09-24-2008, 12:37 PM
|
Array and drop down boxes
|
Posts: 13
Location: UK
|
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')); ?>
|
|
|
|
09-24-2008, 12:38 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 13
Location: UK
|
Ok, what is DOM?
|
|
|
|
09-24-2008, 12:48 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
|
[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?
|
|
|
|
09-24-2008, 12:48 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
|
__________________
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?
|
|
|
|
09-24-2008, 12:52 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
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/
|
|
|
|
09-24-2008, 01:01 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
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.
|
|
|
|
09-24-2008, 01:03 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 13
Location: UK
|
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?
|
|
|
|
09-24-2008, 01:05 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
Maybe you should take a quick tutorial about forms and PHP: http://www.w3schools.com/php/php_forms.asp
|
|
|
|
09-24-2008, 01:07 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 13
Location: UK
|
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.....
|
|
|
|
09-24-2008, 01:47 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 219
Name: Rob
Location: UK
|
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.
|
|
|
|
09-24-2008, 05:02 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 13
Location: UK
|
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
|
|
|
|
09-24-2008, 05:36 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
|
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?
|
|
|
|
09-24-2008, 05:46 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 13
Location: UK
|
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...
|
|
|
|
09-24-2008, 06:01 PM
|
Re: Query about retrieving values from a drop down menu
|
Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
|
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?
|
|
|
|
|
« Reply to Query about retrieving values from a drop down menu
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|