Page 1 of 1

Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Posted: Sat Jul 25, 2009 6:43 am
by mtataric
Hello everybody!

So I have 3 dropdown menus and 2 of them are dependent I made this using JavaScript.. (ex: country, state, province)

They work perfect except when I try to parse the selected items from the dropdown menus they return values like "123, "33", "366"( that is what i see in my MySql database) How do I parse the selectedindex or the "text" instead of the value?

Here a little bit of code:

$sehir = $_POST['sehir'];

$sql = mysql_query("INSERT INTO myMembers (sehir)

VALUES('$sehir)")

or die (mysql_error());

<select name="sehir" class="formFields" id="sehirler" >

<option value="?php print "$sehir"; ?"><?php print "$sehir"; ?></option>

<option value="">&#350;ehir Se&#231;iniz</option>

<option value="1">ADANA</option>

<option value="72">ADIYAMAN</option>

...

...

Thanks in advance!!

~Kaan

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Posted: Sat Jul 25, 2009 8:23 am
by cpetercarter
Three possiblilities:

change the option values in the form so that they become the values you want to post into the database, instead of numbers:

or use some sort of look-up table (eg an array) to convert the numerical value into the value you want to post to the database;

or store the values in the database in numerical form, and convert them to the values you want to use when you extract the values from the database.

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Posted: Sat Jul 25, 2009 8:32 am
by mtataric
Thanks, Awesome suggestions!

I thought about the first option but it is too much work since there is more than 5000 records..

however, your second and third suggestions sound pretty good!

How do I go about doing either one? Could you give me examples :) ?

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Posted: Sat Jul 25, 2009 8:56 am
by cpetercarter
Something like:

Code: Select all

$convert = array (1 => 'text equivalent of value 1',
                         2 => 'text equivalent of value 2',
                         3 => 'text equivalent of value 3'
 
//etc etc for each value you need to convert
 
                           );
 
 
foreach ($convert as $value => $text) {
 
if ($_POST['sehir'] == $value)  {
     $value_to_post_to_database = $text;
     continue;
     }
 
//code to post $value_to_post_to_database to the database!

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Posted: Sat Jul 25, 2009 9:15 am
by mtataric
Great!! I am sure this will work its all a matter of putting all those names into an array without messing up the javascript ..

I appreciate your help!!

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Posted: Sat Jul 25, 2009 10:10 am
by cpetercarter
Sorry, brain not in gear. This is more economical:

Code: Select all

$convert = array (1 => 'text equivalent of value 1',
                        2 => 'text equivalent of value 2',
                        3 => 'text equivalent of value 3'
 
//etc etc for each value you need to convert
 
                           );
 
$value_to_be_posted = $convert[$_POST['sehir']];
 
Make sure that each line of the array, except the last one, ends with a comma.