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="">Şehir Seçiniz</option>
<option value="1">ADANA</option>
<option value="72">ADIYAMAN</option>
...
...
Thanks in advance!!
~Kaan
Parsing Dependent Dropdown SelectedIndex Values into MySQL?
Moderator: General Moderators
-
cpetercarter
- Forum Contributor
- Posts: 474
- Joined: Sat Jul 25, 2009 2:00 am
Re: Parsing Dependent Dropdown SelectedIndex Values into MySQL?
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.
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?
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
?
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?
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?
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!!
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?
Sorry, brain not in gear. This is more economical:
Make sure that each line of the array, except the last one, ends with a comma.
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']];