mtataric Posted July 25, 2009 Share Posted July 25, 2009 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 :-\ Link to comment https://forums.phpfreaks.com/topic/167381-solved-parsing-dependent-dropdown-selectedindex-values-into-mysql/ Share on other sites More sharing options...
haku Posted July 25, 2009 Share Posted July 25, 2009 <option value="?php print "$sehir"; ?"><?php print "$sehir"; ?></option> You are missing '<' and '>' in your php tags. But that being said, the text of a submitted option is not available to php, only the value. Link to comment https://forums.phpfreaks.com/topic/167381-solved-parsing-dependent-dropdown-selectedindex-values-into-mysql/#findComment-882605 Share on other sites More sharing options...
mtataric Posted July 25, 2009 Author Share Posted July 25, 2009 you are right that was a typo when i was posting the question! But someone suggested i have numerical values in MySQL and then maybe convert them to text somehow later on when I need to use those city names to filter and such. How can I do that? Link to comment https://forums.phpfreaks.com/topic/167381-solved-parsing-dependent-dropdown-selectedindex-values-into-mysql/#findComment-882608 Share on other sites More sharing options...
haku Posted July 25, 2009 Share Posted July 25, 2009 I often use something like this: <?php function select_options($index = FALSE) { $options = array('text 1', 'text 2', 'text 3'); if(!$index) { return $options; } if($index >= 0 && $index < count($options)) { return $options[$index]; } return FALSE; } ?> Then, for example, when building a <select> element, I will create it like this: <select name="select_element"> <?php $options = select_options(); foreach($options as $key=>$value) { echo '<option value="' . $key . '">' . $value . '</option>'; } ?> Now lets say that the user selects the first element in the drop down list (text 1). To retrieve and display the human readable value, I use this: <?php echo select_options($index); ?> // where index is the number that has been stored in the database This will return the value of the selected option and print it to the screen. In this way, if I ever want to make changes to the list, I don't have to worry about changing more than one location. Link to comment https://forums.phpfreaks.com/topic/167381-solved-parsing-dependent-dropdown-selectedindex-values-into-mysql/#findComment-882633 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.