Page 1 of 1

Select option issue

Posted: Mon Mar 28, 2011 10:33 pm
by jmoutia
The code below is a just a sample. What I'm trying to do is when I click on the submit button I would like to have the option selected display. Unfortunately for me I'm getting the option value displayed. Can anyone help me please

Code: Select all

<HTML> 
	<head> 
    		<title>Test</title> 
  	</head> 
	<body>
	<form name="test" method="post" action="test.php">
	<select name="myselect" id="myselect">
	<option value="0">Jason</option>
	<option value="1">Sonia</option>
	<option value="2">John</option>
	<input type="submit" value="Submit">
	</select>
	</form>
	</body>
</html>

Code: Select all

<?php

$select = $_POST['myselect'];
echo ($select);

?>

Re: Select option issue

Posted: Tue Mar 29, 2011 6:24 am
by Peter Kelly
The HTML form only sends through the name of the field which in this case is myselect and the value which for example would be 0. If you wanted the name then you will have to rethink your value field.

Code: Select all

<select name="myselect" id="myselect">
        <option value="[0][Jason]">Jason</option>
        <option value="[1][Sonia]">Sonia</option>
        <option value="[2][John]">John</option>
        </select>
        <input type="submit" value="Submit">
Does that return the myselect as an array, Sorry I cant test this at the minute but it should work I think.

Re: Select option issue

Posted: Tue Mar 29, 2011 6:19 pm
by danwguy
Why use an array at all, you can only select one of those options right? so just set the value to the same name as the option. I.e.

Code: Select all

<form action="test.php" method="post">
<select name="myselect" id="myselect">
<option value="jason">Jason</option>
<option value="sonia">Sonia</option>
//... and so on
</select>
</form>
then on test.php

Code: Select all

$select = $_POST['myselect'];
echo $select;
will output the name that they selected.