Forum Moderators: coopster

Message Too Old, No Replies

Question in PHP manual

         

yf_wang

1:50 pm on Apr 16, 2003 (gmt 0)

10+ Year Member



In PHP manual (01-01-2003), there is such an example:

Example 8-4. More complex form variables

<?php
if ($HTTP_POST_VARS['action'] == 'submitted') {
print '<pre>';

print_r($HTTP_POST_VARS);
print '<a href="'. $HTTP_SERVER_VARS['PHP_SELF'] .'">Please try again</a>';

print '</pre>';
} else {
?>
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF'];?>" method="post">
Name: <input type="text" name="personal[name]"><br>
Email: <input type="text" name="personal[email]"><br>
Beer: <br>
<select multiple name="beer[]">
<option value="warthog">Warthog</option>
<option value="guinness">Guinness</option>
<option value="stuttgarter">Stuttgarter Schwabenbräu</option>
</select><br>
<input type="hidden" name="action" value="submitted">
<input type="submit" name="submit" value="submit me!">
</form>
<?php
}
?>

Here I wonder what is the purpose of the hidden input
<input type="hidden" name="action" value="submitted">

that is used in the if statement.

What is the difference from using if($submit) as normally encoutered?

andreasfriedrich

2:15 pm on Apr 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The extra hidden field is just used to test whether the FORM [w3.org] was submitted. The same could be done by testing for $_POST['submit'] == "submit me!". If you have different submit buttons then using a hidden field as in the example is easier to test for than all those different submit buttons.

$_SERVER['REQUEST_METHOD'] == 'POST' is another way to check whether the form was posted or not.

Andreas