tags:

views:

60

answers:

3

What is the code for a PHP form that matches a user input to a correct response and if it is not equal, it returns an error and if it is it shows a successful message.

Example: If the proper string to insert is 1234 and the user enters 3948, it should return an error, but if the user enters 1234 it should go to a new page.

+1  A: 

Make a form that calls a php script as its action and in that php script test the value of the field against the '1234' or whatever you wanted.

if($_POST['variable'] == '1234'){ 
    header('Location: http://www.site.com/success'); 
}else{
    echo 'Error Message';
}
thisMayhem
Throw a little header action in there and it completes all the requirements.http://php.net/manual/en/function.header.php
Mike Keller
True, thank you! Done :)
thisMayhem
A: 

Without wasting too much time, something like:

$answer = intval($_REQUEST['ans']);
if ( $answer == 1234 ) {
  header("Location: newpage.php");
} else {
  // add whatever you need to complete
  // the page
  echo "Error";
  // add whatever you need to complete
  // the page
}

can be a starting point.

ShinTakezou
A: 

Something like this should do the trick:

<?php
if($_POST['user_input'] == "1234"){
  header("Location: correct.php");
}elseif(isset($_POST['user_input'])){
  echo "Incorrect!";
}
?>

<form method="post" action="<?php echo $PHP_SELF;?>">
Input:<input type="text" name="user_input">
<input type="submit" value="Click!">
</form>
john