With PHP, you can get data from forms and work with that data to achieve various things.
This tutorial focuses on:
- Getting data from a user submitted form
- Declaring variables for the data from a user submitted form
- Displaying the data from a user submitted form on a webpage
- Different methods of sending data
- Sending data to a page without using a form
Getting data from a user submitted form
Getting data from a user submitted form requires at least one page - that is, the page from which the form was submitted.
Take a look at this code for a form:
By its code, the above form cannot be submitted. The following code contains a modified version of the above form which makes it possible to submit it:
We now know that when this form is submitted it will be submitted using the POST method, the data will be sent to a page called page2.php and there will be two variables sent to page2.php - those variables being the values from the fields in the form. Whenever a form is submitted, the number of fields in it will be the number of variables sent to the page to which it is submitted.
Declaring variables for the data from a user submitted form
So now that the data is submitted, how do we declare variables for it? How do we get that data? We must know the name of the submitted forms fields to do so. In the source code above there were two fields - one named 'name' and the other named 'aNumber'.
The $_REQUEST variable is an associative array which holds all of a submitted forms values. A variable is declared set to equal the value of a certain form field.
In the above example two variables are declared. The first variable gets the value of the form field named "name". The second variable gets the value of the form field named "aNumber".
Displaying the data from a user submitted form on a webpage
Displaying the data on a webpage from a user submitted form involves printing the appropriate variables.
Different methods of sending data
The form presented earlier in this lesson was submitted using the POST method. There is another method that can be used for form submission and that is the GET method.
The difference between the POST and the GET method:
The POST method puts the values from a form into the headers of an HTTP communication, while The GET method puts the values from a form right into the URL.
When getting data from a submitted form, the $_REQUEST variable is not the only choice. When a form is submitted using the GET method, you can use the $_GET variable, and when a form is submitted using the POST method, you can use the $_POST variable. The $_REQUEST variable can be used for any form, whether it is submitted using the GET method or the POST method.
Sending data to a page without using a form
It is possible to send data to a page without using a form. This is accomplished by supplying the appropriate data in the URL itself.
Searching for the term "apple" with the search engine at www.google.com yields a page with the URL http://www.google.com/search?hl=en&q=apple&btnG=Google+Search
The search string 'apple' was sent to the google search page. In this case, sending data to a page without using a form would mean manually altering the URL by putting the search term in it without actually using the search form.
A URL such as http://www.google.com/search?hl=en&q=orange&btnG=Google+Search can be used for this task.
Entering this URL into a web browser would run the search through the google search engine and return the result, without having to use the search form.