PHP: Using hidden parameters to test for form submission

Another technique for testing form submission is to use hidden parameters. If the hidden parameter is in $_POST , then we process the form.

<input type='hidden' name='submit_check' value='1'>

The hidden parameter is shown in line 25 and there is a type=’hidden’ in the parameter.

<?php

// Logic to do the right thing based on
// the hidden _submit_check parameter
if ($_POST['submit_check']) {
    process_form();
} else {
    show_form();
}

// Do something when the form is submitted
function process_form()
{
    print 'Hello, ' . $_POST['my_name'];
}

// Display the form
function show_form()
{
    print<<< html_output
<form method='POST' action='$_SERVER[PHP_SELF]'>
Your name: <input type='text' name='my_name'>
<br/>
<input type='submit' value='Say Hello'>
<input type='hidden' name='submit_check' value='1'>
</form>
html_output;
}

?>
Series Navigation<< PHP: Form processing with functions
PHP: A simple form with validation >>