Once you have created the form that the user fills in, you now need the page that does something with the information. The page itself will include PHP code in order to deal with the form information, but other than that can have any other content on it as well.
As we are assuming you know how to write basic HTML, the examples below will just illustrate how to use the entries your user put into the previous page's form in this your page. To recap, this secondary page that features the values your user input must be the page mentioned in the "action" attribute of the form they filled in, and the user must have filled in the form and clicked submit to arrive at this page. The key thing here is that you cannot simply test your page by loading it without filling in the form on the first page and pressing submit.
PHP has a type of variable called superglobals. These may sound rather comic-book-characterish but the important thing is that these contain a whole pile of information that may come from outside the immediate page's code that you are working on. Two key ones are the 'post' and 'get' superglobal. These are referred to in the following manner.
$_GET['variable']
$_POST['variable']You may notice that the names of these superglobals correspond with the two methods of form actions mentioned in the previous section. There, you got to choose whether your form would work with the 'get' or the 'post' method; here you need to remember which one you chose. The examples from now on will assume you chose the 'get' method on the form. If you in fact chose post, simply replace all references to $_GET to $_POST.
It was mentioned that we referred to the information contained within the get superglobal in the following manner: $_GET['variable']. The word 'variable' is intended to show that that part of the statement changes depending on what piece of information, in this case from the form, you are interested in. The important thing to note is that you should replace "variable" with the name you assigned to the particular control on form that the user filled in on the previous page you are interested in exploring.
For instance, if on your form page you had made a control like:
<input type="text" name="searchfor" />On the page that deals with the results of the form, you can refer to the value of that control like this:
$_GET['searchfor']If the user had filled in the searchfor textbox with "apple" before pressing submit, that would therefore mean that $_GET['searchfor'] actually represented "apple". In fact, were you to use the below bit of code on the second page
<?php
print "$_GET['searchfor']";
?>The user would see the word "apple" onscreen.
You can simply replace the searchfor bit of the $_GET with other names of your controls to find what the user has chosen to do with their form.
Making it simpler to process
You might have noticed that $_GET['searchfor'] is rather longwinded to type. Furthermore, strange symbols like $ and ' can accidentally mess up certain other stuff you might try to do. This is particularly in the case of using databases, where without slight attention, putting quotes into a database command could make it go rather haywire. For both these reasons it is often sensible to in effect rename $_GET['searchfor'] to something else.
For programmers, this is simply meant to indicate assigning the value of $_GET['searchfor'] to a more sensibly named variable. Non-programmers can just think of it as copying $_GET['searchfor'] into something else.
An example:
$searchfor = $_GET['searchfor'];The above code means "take whatever $_GET['searchfor'] represents and make $searchfor represent it as well". In PHP it is important that all variable - places you can store information - names start with a $.
After the above, $searchfor now has the same value as $_GET['searchfor']. This means that the below code will now display "apple" on the webpage if that's what the user typed into the form textbox control that had the name 'searchfor' on the previous page.
<?php
$searchfor = $_GET['searchfor'];
print "$searchfor";
?>More usefully, you can combine these results with other variables to make more complicated expressions. For instance, in sort-of database-speak:
$sql = "SELECT price FROM shop WHERE item = '" . $searchfor . "'"Would end up making $sql have the value of "SELECT price FROM shop WHERE item = 'apple'" when run. (Non-PHP people - the dot (.) is a concatenation operator - that is to say it just joins two strings of text together).
An exception - Checkboxes
Whilst it is not going to doom you to failure to do the above generally, if you are using checkboxes you might receive strange warnings about "undefined variables" depending on your host. A better way to deal with checkboxes is to use the PHP isset($variable) function.
This function is either going to return true or false. You give it a variable name, and if the variable in any way exists it becomes true, if it does not then it is false. As we learned in the previous section that
"if a simple checkbox is ticked when the form is submitted, its name will be available as a variable to the next page. If not, then it won't exist in any way"
the following is a way of working out whether it was ticked or not on the second page.
if (isset($_POST['emailme']))
{
$emailme = "yes";
}
else
{
$emailme = "no";
}At the end of that, $emailme will either represent the word "yes" if the box was ticked, or "no" if it was not.
Previous section - Setting up the form
Back to start - Using forms in PHP

Recent comments
1 year 2 weeks ago
1 year 6 weeks ago
1 year 7 weeks ago
1 year 7 weeks ago
1 year 7 weeks ago
1 year 7 weeks ago
1 year 7 weeks ago
1 year 8 weeks ago
1 year 8 weeks ago
1 year 9 weeks ago