Parsing Dependent Dropdown SelectedIndex Values into MySQL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mtataric
Forum Newbie
Posts: 3
Joined: Sat Jul 25, 2009 6:41 am

Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Post 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
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Post 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.
mtataric
Forum Newbie
Posts: 3
Joined: Sat Jul 25, 2009 6:41 am

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Post 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 :) ?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Post 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!
mtataric
Forum Newbie
Posts: 3
Joined: Sat Jul 25, 2009 6:41 am

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Post 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!!
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?

Post 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.
Post Reply