Jump to content


Photo

checking a form


  • Please log in to reply
1 reply to this topic

#1 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 13 May 2006 - 04:02 PM

im having trouble checking this form
<form action="check.php" method="post">
Secret Question*:
<select name="starter">
<option value="pikachu" selected="selected">pikachu</option>
<option value="bulbasaur" selected="selected">bulbasaur</option>
<option value="squirtle" selected="selected">squirtle</option>
<option value="charmander" selected="selected">charmander</option>
<option value="zapdos" selected="selected">zapddos</option></option></select>
<INPUT type=submit value=Sign-up>
</form>

there are 4 real options the last 1 zapdos is invalid

<?php
$starter = $HTTP_POST_VARS["starter"];
?>
<?php
$choosable = array();
$choosable[] = 'pikachu';
$choosable[] = 'charmander';
$choosable[] = 'bulbasaur';
$choosable[] = 'squirtle';
foreach($choosable as $choice) {
    $starter2 = $starter;
    if($starter2 != $choice){
        echo "You may not choose a starter that is not on the list!";
        exit();
    }
}
?>

thats the code thats supposed to check if its invalid but it gives the invalid error no matter which 1 i choose

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 13 May 2006 - 04:28 PM

Try this one, note that it's case sensitive:
<?php

if(!empty($_POST["starter"]))
{
$starter = strip_tags($_POST["starter"]);
$choosable = array("pikachu","charmander","bulbasaur","squirtle");

if (!in_array($starter, $choosable))
{
echo "You may not choose a starter that is not on the list!";
exit();
}
else
{
  // ok
}

}
else
{
  // $starter contains no data
}

?>

Note: Since PHP 4.1.0 you should use $_POST instead of $HTTP_POST_VARS when retrieving posted variables (also on GET, COOKIE etc, consult the [a href=\"http://no.php.net/manual/en/reserved.variables.php\" target=\"_blank\"]manual[/a]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users