Submission Button

We mentioned that the submission button was missing. Now's the time to throw it into the existing code. The button is the same as any submission button, the only thing we need to be sure to add is a name to it so we can call it later using PHP.

Code:
...
<input type="submit" value="submit" name="submit"><br />
</form><br />

Retrieving Form Data - Setting up Variables

In PHP there lies an array used to call data from our form. It's a superglobal of PHP and it's one that is great to have memorized. $_POST retrieves our form data and output's it directly to our browser. The best way to do this, is to make variables for each element in our form, so we can output this data at will, using our own variable names. Place the following lines of code at the top of your form file using the correct PHP syntax.

Code:

<?php
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$gender = $_POST["gender"];
$food = $_POST["food"];
$quote = $_POST["quote"];
$education = $_POST["education"];
$TofD = $_POST["TofD"];
?>

All we are doing here is making easier variable names for our form output. With the above statements, we can call our data with ease! Any capital letters under the name attribute must match up with your statements above, avoid overly complicated names to simplify your debugging process and it can save you some frustration as well.

$PHP_SELF; - Submission

For the form action, we will call PHP's $PHP_SELF; array. This array is set up to call itself when submitted. Basically, we are setting up the form to call "formexample.php", itself. Here's a glimpse of how to do just that.

Code:

...
$quote = $_POST["quote"];
$education = $_POST["education"];
$TofD = $_POST["TofD"];
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
...

We now have a completed form ready to recieve data and display results. However, we need to adjust things so that once the data has been submitted we are directed to the results. Typically, we have a completely new .php file that recieves our HTML form data. In this scenerio, we will use an if statement to display first our form, and then our form results upon submission. This is a practical method when entering information into databases as you learn more.

For now here's a look at our complted form document thus far.

Code:

<?php
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$gender = $_POST["gender"];
$food = $_POST["food"];
$quote = $_POST["quote"];
$education = $_POST["education"];
$TofD = $_POST["TofD"];
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
Gender:<br />
Male:<input type="radio" value="Male" name="gender"><br />
Female:<input type="radio" value="Female" name="gender"><br />
Please choose type of residence:<br />
Steak:<input type="checkbox" value="Steak" name="food[]"><br />
Pizza:<input type="checkbox" value="Pizza" name="food[]"><br />
Chicken:<input type="checkbox" value="Chicken" name="food[]"><br />
<textarea rows="5" cols="20" name="quote" wrap="physical">Enter your favorite quote!</textarea><br />
Select a Level of Education:<br />
<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select><br />
Select your favorite time of day:<br />
<select name="TofD" size="3">
<option value="Morning">Morning</option>
<option value="Day">Day</option>
<option value="Night">Night</option></select><br />
<input type="submit" value="submit" name="submit">
</form>