PHP forms on a single page

Rather than creating two pages to display and process a form it is possible to use just one. This does complicate the structure of the page but also allows you to do more later on (in the area of security).

Choosing whether to display or process

The basic concept is an if statement:

Create a new PHP page called formandprocess.php and insert the start and end of the PHP tag (<?php and ?>). Inside put a properly indented IF statement:

if (!$_POST) {
     

} else {
    
    
}
        

The first time the page is loaded in your browser the $_POST super global will be empty. So the IF condition (which is that $_POST is NOT there) succeeds and the form is displayed. The ELSE is not used.

The IF - an HTML form

Paste the HTML code from form1.php between the IF and the ELSE. The problem is that this is HTML not PHP so it will not work as it is. You have two choices. One is to switch off from PHP mode (?>), paste in the HTML and then open the PHP again (<?php):

?>
<form method="post" action="formandprocess.php">
    <p><label for="firstname">Type your first name here: </label><input type="text" name="firstname" id="firstname" /></p>
    <p><label for="surname">Type your surname here: </label><input type="text" name="surname" id="surname" /></p>
    <p><label for="dob">Your date of birth (dd/mm/yyyy): </label><input type="text" name="dob" id="dob" /></p>
<input type="submit" name="submit">
</form>
<?php
        

The other way is to ECHO all of the HTML (being sure to escape all of the quotes with a slash). This would be more efficient but also is a lot of work:

        
echo "<form method=\"post\" action=\"formandprocess.php\">
    <p><label for=\"firstname\">Type your first name here: </label><input type=\"text\" name=\"firstname\" id=\"firstname\" /></p>
    <p><label for=\"surname\">Type your surname here: </label><input type=\"text\" name=\"surname\" id=\"surname\" /></p>
    <p><label for=\"dob\">Your date of birth (dd/mm/yyyy): </label><input type=\"text\" name=\"dob\" id=\"dob\" /></p>
<input type=\"submit\" name=\"submit\">
</form>";
        

Note the change the form ACTION.

The ELSE - processing the data

The code from form1process.php needs to go into the ELSE part of the IF statement. No changes are needed as it is PHP code.

Save the new file as formandprocess.php. Upload it and try it. You should not be able to see any difference comparing it with the two page method except by looking in your browser's address bar.

PHP_SELF

So far you have put the name of a PHP page in the form action. If you then change the name of the PHP page (maybe you copy the form for use on another site) you need to remember to change the action as well. Instead you could refer to the current page using the $_SERVER super global which includes the name of the page. First put a new line immediately after the IF line but before the ECHO:

$filename=$_SERVER["PHP_SELF"];                            
        

Then change the form ACTION to this:

<form method=\"post\" action=\"$filename\">                            
        

This particular line of code will only work if the form is ECHOed in PHP rather than stopping and starting PHP processing as shown above.

submit to reddit Delicious Tweet