In the HTML tutorial you learned how to create HTML forms.  They are used to get data from the user to the server.  You almost know enough for that to be useful now.

Open your page forms.html and save it as phpformentry.html.  It does not need a PHP extension as it is just going to be a form.  If you did not create that page you can do it now. Check that it still works (it should repeat back what you put in the form fields.

The data from the form will be processed on a second page.  Open phpifelse.php and save it as phpformprocess.php.  Now you will edit both those files.

The pages will be a log in system (part of one).  The first will get the user to type in their username and password.  The second will check to see if that is correct.


Change the action from:

<form method="post" action="">


<form method="post" action="phpformprocess.php">

If you test the form now it will not work properly as the browser will try to send the data to the server but phpformprocess.php has not been finished yet.



The first thing you need to know is that PHP makes it very easy to get at data which has been sent to the server in a form.  There is a special variable in PHP called $_POST.  This contains all the data sent in a form.  To prove this paste this code just before the first line of PHP ($firstvalue) in the page (just after the opening of the <?php tag):


Now upload BOTH pages to your server.  Then open the form entry page in your browser.  Fill in the form fields (any text) but do not submit it.  Before you do anything else look at the address bar of your browser.  It should be showing the server name or address and then the page name (phpformentry.html).  Press the submit button now and then look again at the address.  When a user submits a form the server receives the data and looks for the named page (from the action attribute).  It uses the code in that named page to process the data.  In your browser you should now see something like this:

Array ( [text1] => fred [text2] => secret )

The data has gone to the server and the second page has taken it from the $_POST variable and used print_r to display it.  The display can be a little confusing but it shows the name of the first field (text1) and then what was in it (fred in my example).  Then the name of the second field and what was in it.  The word Array is there but for now ignore it.

You now know that your form is working and it is recommended that you test all forms using print_r and $_POST before you try using the data.


Now you know that your form is right you can process the data by adapting the code already there.  Delete the print_r line first as it was just a test.

Replace the first line (starting $firstvalue) with this:


It takes just one of the pieces of data and puts it into the variable.  Change the second line to do the same but with the password (text2).  You now have the username and password in variables so you can use them in the if statement. 

Replace the condition of the if statement (the bit inside normal brackets) with this condition:


If the username is fred the condition is true.  Try it with correct and incorrect usernames and passwords to spot the problem (if you missed it).

To check the password as well put a new if statement inside the first one.  This is a nested if so you will have this:

if ($firstvalue=="fred") {
    if ($secondvalue=="secret") {
        echo "<p>the values are the same</p>";
    } else {
        echo "<p>the values are different</p>";
        echo "<p>sorry about that</p>";
} else {
    echo "<p>the values are different</p>";
    echo "<p>sorry about that</p>";

There is a better structure than this but that will come later.   This works and only uses code you have learned already.  Test yours.

The messages which are displayed are also not great but you can fix those.  Replace the echo lines with more appropriate messages.