Saturday, January 1, 2011

php- form submit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
 <title>Untitled</title>
</head>
<body>

<?php
/* if the "submit" variable does not exist,
 * the form has not been submitted - display initial page */
if(!isset($_POST['submit'])){
?>
    <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
    Enter your age: <input name="age" size="2">
    <input type="submit" name="submit" value="Go">
    </form>
<?php
}
else{
/* if the "submit" variable exists,
 * the form has been submitted - look for and process form data

 */    // display result
    $age = $_POST['age'];
    if($age >= 21){
        echo 'Come on in, we have alcohol and music awaiting you!';
    }
    else{
        echo 'You\'re too young for this club,
  come back when you\'re a little older';
    }
}
?>

<!--
Use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'].
because
$_SERVER['PHP_SELF'] will contain not just login.php, but the entire login.php/nearly_arbitrary_string.
-->

</body>
</html>

No comments:

Post a Comment