View Full Version : GET selectedIndex number

01-07-2010, 09:16 PM
I'm in need of passing the selectedIndex number of a select menu from a form to a PHP page. I can pass the actual value of the select menu using:

$State = $_POST['State'];

How would I get the selectedIndex number?

01-07-2010, 09:23 PM
PHP is server side, it has no idea there are even other items to look at. You can use Javascript to alter the value sent over from the form into php if you wanted to.

01-07-2010, 09:41 PM
Thanks for the reply. Can you help shed some light on how to do that? I know Javascript to a point. Its probably something easy that I'm glossing over.

01-07-2010, 09:42 PM
Create a hidden input in your form. Give it an id and a name. Then use onchange in your select menu and set the value of that hidden input using document.getElementById('yourinput').value = this.selectedIndex;

01-08-2010, 10:31 AM
You may store all the options and indexes as a PHP associative array and then get the key from the dropdown value using array_search() (http://php.net/array_search)