Problem with $_POST (I think)

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
Moriarty
Forum Newbie
Posts: 8
Joined: Tue Aug 22, 2006 9:33 am
Location: London, England. Home of Dangermouse.

Problem with $_POST (I think)

Post 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.
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

You realize your missing </option> tags all over the place?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The new code uses an assignment operator ("=") instead of an equality ("==") or identity ("===") operator.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

The SELECTED attribute doesn't take an assignment.
Moriarty
Forum Newbie
Posts: 8
Joined: Tue Aug 22, 2006 9:33 am
Location: London, England. Home of Dangermouse.

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

double quote your html attributes.
sinlge quote your php strings.
Post Reply