Page 1 of 1

Return the selected option as a number

Posted: Tue Sep 20, 2005 11:00 am
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>

Posted: Tue Sep 20, 2005 11:09 am
by feyd
you'd have to submit the form. If no value attribute is specified, the offset into the option list is sent.

Posted: Tue Sep 20, 2005 1:59 pm
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.