...

View Full Version : PHP <select><option> problem



Harry
10-12-2003, 11:38 PM
Ok, here is my problem. I have a drop down menu where I select an option. and then submit the form. What I want to do is when I submit the form is tag the option as selected so it will display the
value every time I submit the form unless I select another option
and then I want to keep that value.

Here is some basic code I have I written but for some reason
it does not work. Please excuse the sloppy code, I threw this together in a hurry.

<?php
if($tt != "2")
$veg="Avacado";
?>
<html>
<head><title></title></head>
<?php
echo"
<p align='center'>
<body>
<form action='$PHP_SELF' method='get' name='my_form'>
<select name='veggies'>";
?>
<option value=<?php echo $veg; ?> <?php if($_POST['veggies'] == $veg) $slct= "selected"; ?>><?php echo $veg; ?></option>
<?php
echo "
<option value='Lettuce', $slct>Lettuce</option>
<option value='Corn'>Corn</option>
<option value='Tomato'>Tomato</option>
<option value='Spinach'>Spinach</option>
<option value='Broccoli'>Broccoli</option>
</select><br><br>
<input type='submit' value='Submit'>
<input type='hidden' name='tt' value='2'>
<input type='hidden' name='veg' value='$veggies'><br><br>
</p?";
?>

</form>
</body>
</html>

This is what is happening:
The first time I load the page, the "Avacado"
option is displayed.

Now I select the option "Tomato" and
click on the submit button.

Now the "Lettuce" option is displayed.

I now click the submit button again
and the "Tomato" option is displayed.

I now click the submit button again
and the "Lettuce" option is displayed.

I now click the submit button again
and the "Tomato" option is displayed.

Every time I click on the submit button, the options keep flip-flopping between "Lettuce" and "Tomato". I want the
"Tomato" option to stay on the screen unless I
select another option then I want that option to remain on the screen until I change it.

Could someone please help me and tell me what I am doing
wrong here.

Thank you.

Spookster
10-13-2003, 12:38 AM
It's as simple as this





<?php
$item = $_POST["item"];
?>

<select name="item">
<option <?php if($item == "item1") echo "selected"; ?> value="item1">Item 1</option>
<option <?php if($item == "item2") echo "selected"; ?> value="item2">Item 2</option>
</select>

SDP2006
10-13-2003, 01:37 PM
Whatever is between the <option></option> is the value. i.e if I had
<select name="words">
<option>Hello</option>
<option>Hello World!</option>
</select>
and you hit submit with "Hello World" selected, then $_POST['words'] would have a value of Hello World!

Harry
10-13-2003, 02:38 PM
Thanks a million guys. I couldn't have done it without you. You saved the day for me. This works perfectly now.

Spookster
10-13-2003, 03:05 PM
Originally posted by SDP2006
Whatever is between the <option></option> is the value. i.e if I had
<select name="words">
<option>Hello</option>
<option>Hello World!</option>
</select>
and you hit submit with "Hello World" selected, then $_POST['words'] would have a value of Hello World!

You need to lay off the crack pipe.

You need to specify the value using the value attribute.

SDP2006
10-13-2003, 03:06 PM
Oh hah hah

Nightfire
10-13-2003, 03:33 PM
Originally posted by Spookster
You need to lay off the crack pipe.


hahaha :thumbsup: Best laugh I've had in ages



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum