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.

Link to comment
Share on other sites

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);
Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.