Select option issue

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
jmoutia
Forum Newbie
Posts: 1
Joined: Mon Mar 28, 2011 10:21 pm

Select option issue

Post 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);

?>
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: Select option issue

Post 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.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Select option issue

Post 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.
Post Reply