- slide 1 of 4
Creating the HTML form
In previous articles, we talked about PHP and its syntax, created a working script, executed it and looked at variables. Now lets put some of this to use and create an HTML form that we will use to collect input from the user. We'll then use variables to store the input from the user and then perform awesome tricks with it using PHP.
We'll create a web page that takes a message from the user and shows it on a different page.
Lets begin with the HTML page that'll contain the form. Call it "message_form.html" - if you want to call it something else, you may! For easy reading see this paste bin.
<form action="message_tutorial.php" method=post>
What is your name?:
<br> <input type="text" name="FirstName">
What is your message?
<br /><input type="text" name="Message">
<input type="submit" name="submit" value="Submit">
- slide 2 of 4
Above is just a plain old static HTML form, below points out the juicy bits:
The HTML <form action="message_tutorial.php" method=post> dictates which PHP script will process the input from the from, in other words, it's what makes the form work or do anything at all! Post is the method in which the data is sent from the form to the PHP script; we shall look at this later.
Input type="text" creates a text box element on the form (where the user inputs their data). Name="FirstName" - The data that the user inputs here will be stored in a variable called "YourName." Can you start to see the importance of variables in forms now?
<input type="text" name="Message"> creates another text field that creates a variable called "Message" that will store the users message.
<input type="submit" name="submit" value="Submit"> creates a submit button, that the user will click.
</form> ends the form.
So now we have a way of collecting the user's input, we need to do something useful with it! You can use the echo or print command to output the variables contents on another page. In some cases it might be preferable to do all of this in one PHP script.
When the user clicks the submit button "message_tutorial.php" is loaded. Here is "message_tutorial.php" or see this code in this paste bin:
<title>Here is your message</title>
// Get the values from the form, "FirstName" and "Message"
$FirstName = $_REQUEST['FirstName'] ;
$Message = $_REQUEST['Message'] ;
Hey, <?php print $FirstName; ?>
Your message is <b> <?php echo $Message; ?></b>
- slide 3 of 4
Do I use GET or POST?
You might have noticed that we used the POST method to pass data from the form to the PHP script <form action="message_tutorial.php" method=post>. However, we could have used GET instead.
The difference between POST and GET? Well, POST is sent as plain text in the body of the request and is not visible to the user, whereas, GET will send the information as plain text in part of the URL, for example:
Notice the variable names and values being passed!
- slide 4 of 4
We now have a working form that passes variables to our PHP script. In the next part of this article series I will teach you about PHP arrays, this will help you to pass the form values to other scripts or to easily post the form values to a database.
A Guide to Linux Programming with PHP
- A Guide to Linux Programming with PHP
- A Guide to Linux Programming with PHP - Commenting and Executing Code
- A Guide to Linux Programming with PHP - Variables & Data Types
- A Guide to Linux Programming with PHP - HTML Forms and PHP
- A Guide to Linux Programming with PHP - Arrays
- A Guide to Linux Programming with PHP - Associative and Multidimensional Arrays