Jump to content

[SOLVED] Parsing Dependent Dropdown SelectedIndex Values into MySQL?


mtataric

Recommended Posts

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 :-\

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.