Jump to content

Basic: how to find out the selected item of a listbox?


ixnayz

Recommended Posts

Hi, I'm coding a page that starts with a select list loaded from a database and I want to make a different query depending on the item selected, I have been searching around and I can't find a php version of the selectedIndex of a listbox in javascript, any idea? thanks.

First thing you should do is stop trying to think of it in terms of things work with .NET and learn from scratch how it works with PHP. There is no SelectedIndex or anything like that.

 

In $_GET or $_POST (depending which you're using) will be an item corresponding to the name of the you used. The value of that item will be the selected value from the .

<form method="post">
	<select name="dropdown">
		<option value="1">One</option>
		<option value="2">Two</option>
		<option value="3">Three</option>
	</select>
</form>
echo $_POST["dropdown"]; // 1, 2, or 3
[edit] You also mentioned Javascript, but it sounded like a PHP question. The Javascript version is, without a framework, just .value on the select element.

alert(document.forms[0].dropdown.value);

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.