Page 1 of 1

Problem with $_POST (I think)

Posted: Wed Aug 23, 2006 11:08 am
by Moriarty
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):

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

 ?>
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:

Code: Select all

if($value=$Dvalue)
    {
        echo "<option value='".$value."' selected='selected'>".$value;
    }
    else
    {
        echo "<option value='".$value."'>".$value;
    }
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.

Posted: Wed Aug 23, 2006 11:54 am
by LiveFree
You realize your missing </option> tags all over the place?

Posted: Wed Aug 23, 2006 12:15 pm
by feyd
The new code uses an assignment operator ("=") instead of an equality ("==") or identity ("===") operator.

Posted: Wed Aug 23, 2006 12:39 pm
by Buddha443556
The SELECTED attribute doesn't take an assignment.

Posted: Wed Aug 23, 2006 1:11 pm
by Moriarty
Thanks once again. I know there were unclosed tags but was more concerned with why things weren't working properly.

Job now done. Customary thanks to all.

Posted: Wed Aug 23, 2006 5:17 pm
by Ollie Saunders
double quote your html attributes.
sinlge quote your php strings.