Return the selected option as a number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
quadoc
Forum Contributor
Posts: 137
Joined: Fri Jul 01, 2005 5:33 pm
Location: Atlanta, GA

Return the selected option as a number

Post by quadoc »

I've the following code and I want to extract the return selected index to use in php code, does anyone know how to do this? Please post some tips. Thanks... :?

Code: Select all

<html>
<head>
<script type="text/javascript">
function getIndex()
{
var x=document.getElementById("mySelect")
alert(x.selectedIndex)
}
</script>
</head>

<body>
<form>
Select your favorite fruit:
<select id="mySelect">
	<option>Apple</option>
	<option>Orange</option>
	<option>Pineapple</option>
	<option>Banana</option>
</select>
<br /><br />
<input type="button" onclick="getIndex()" value="Show index of selected fruit">

<?php

  echo "what is the selected index? =";

?>

</form>
</body>

</html>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'd have to submit the form. If no value attribute is specified, the offset into the option list is sent.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

without submission of a form, dhtml basically the client knows wot the values are in the form controls.
but there is no possibility for the server to know the values until you pass them through form submission.
Post Reply