Jump to content

checking a form


desithugg

Recommended Posts

im having trouble checking this form
[code]<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>[/code]

there are 4 real options the last 1 zapdos is invalid

[code]<?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();
    }
}
?>[/code]

thats the code thats supposed to check if its invalid but it gives the invalid error no matter which 1 i choose
Link to comment
Share on other sites

Try this one, note that it's case sensitive:
[code]
<?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
}

?>
[/code]

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.