In this lesson, we will look at how to build forms and process inputs on the server.
<form>
When you code a form, there are two particular important attributes: action and method.- action
- Is used to enter the URL where the form is submitted. It would be the PHP file that you want to handle the input.
- method
- Can either have the value "post" or "get", which are two different methods to pass data. At this point, you don't need to know much about the difference, but with "get", the data is sent through the URL, and with "post", the data is sent as a block of data through standard input service (STDIN). In the last lesson, we looked at how data is retrieved through the URL using $_GET. In this lesson, we look at how data submitted through a form using the method "post" is retrieved.
An HTML page with a form
The page that contains the form doesn't need to be a PHP file (but it can be). It need not even be on the same site as the file that will receive the data.In our first example, we will look at a very simple form with one text field:
The result in the browser is a form:
Now we come to the fun part: receiving and handling the data with PHP.
Requesting form data with PHP
When you need to request data submitted through a form (post method), you use $_POST:Which returns the value of a field in the form. Let us try to use it in an example.
First create a page with a form as above. Then make a PHP page named "handler.php" (notice that this is the name of the page we wrote in the action attribute in our <form>).
The file "handler.php" shall have the following content:
show example
User input and conditions
In the next example, we will try to use user input to create conditions. First, we need a form:Which will look like this in the browser:
Now we will use these inputs to create a page that automatically changes background color according to what the user's favorite color is. We can do this by creating a condition (see lesson 6) that uses the data the user has filled out in the form.
The background color will be white if the user has not chosen any favorite color in the form. This is done by using default to specify what should happen if none of the above conditions are met.
But what if the user does not fill out his name? Then it only says "Hello" in the title. We will use an extra condition to change that.
show example
In the example above, we use a condition to validate the information from the user. In this case, it might not be so important if the user did not write his name. But as you code more and more advanced stuff, it's vital that you take into account that the user may not always fill out forms the way you had imagined.
Example: contact form
With your new knowledge of PHP and forms, you are able to create a contact form using the function mail, which has the following syntax:First, we need a simple HTML form:
<html> <head> <title>Contact form</title> </head> <body> <h1>Contact form</h1> <form method="post" action="handler.php"> <p>Subject:<br /><input type="text" name="subject" /></p> <p>Message:<br /><textarea name="message"></textarea></p> <input type="submit"> </form> </body> </html>
Please note that the example will only work if you have access to a mail server. By default, this is not the case in XAMPP and most free hosts. Also, some hosts may require that you include a from header, which is done with an extra parameter:
mail("you@yourdomain.com", "Test", "This is a test mail", "From: me@mydomain.com"); |
---|