Problem with $_POST (I think)
Posted: Wed Aug 23, 2006 11:08 am
Thanks for the help yesterday and I'm back again with another sanitised problem:
I have a select list on a form and would like the form to post back to itself whilst retaining the values that have been selected. I have been able to do this with text inputs but have had no joy with my select list.
This works well (but without retaining the value):
However, when I replace the line of code indicated above with something I think should work, it goes horribly wrong. Here is the code I'm trying to swap in:
Instead of my nicely formatted select list, I get the selected value copied n times.
If anyone has any light to shed, I'd be much obliged.
I have a select list on a form and would like the form to post back to itself whilst retaining the values that have been selected. I have been able to do this with text inputs but have had no joy with my select list.
This works well (but without retaining the value):
Code: Select all
<?php
$Cats=array("Tom","Ginger","Barney","Snowball i","Snowball ii");
if(isset($_POST)){
$Dvalue=$_POST['CatValue'];
echo $Dvalue;
}
echo "<form method=\"POST\" action=\"CatsExample.php\">";
echo "Select that cat: <select name='CatValue'><BR><option value='Select From List'>Select From List";
foreach ($Cats as $value)
{
//I'm going to replace this next line in my second non-working exmple
echo "<option value='".$value."'>".$value;
}
echo "</select>";
echo "<input type='submit' accesskey='s' name='post' class='mainoption' value='Submit' /><P>";
print_r($_POST);
?>Code: Select all
if($value=$Dvalue)
{
echo "<option value='".$value."' selected='selected'>".$value;
}
else
{
echo "<option value='".$value."'>".$value;
}If anyone has any light to shed, I'd be much obliged.