PHP: A Beginners Guide - Part 3
Thursday, August 28th, 2008 | PHP
Introduction
Today we will go over a more complex concept and really get our hands wet. We are going to go over something that is the core of all server side dynamic websites. Parsing form input.
For a little refresher I am going to define some terms that will be used throughout this article.
Form: The XHTML tag used to define the area of feild(s) where users can input values.
Action: The URL the browser redirects to after the user submits the form (usually a script that processes the data).
Method: The “method” used to submit the form to the server.
GET: The form method where the input is sent through the URL (ex: process.php?FormName1=Value1&FormName2=Value2).
POST: The form method where the input is sent through a message body (hidden to end users).
Query string: The portion of the url after the file extension which contains the data (ex ?FormName1=Value1&FormName2=Value2).
Today we will discuss when to use GET or POST methods, how to send Data through the URL without a form, How to retrieve GET and POST data using PHP, and how to display the data on your web page.
You can download all the example files for today’s tutorial here. You can also view it live by clicking here.
A Basic XHTML Form
Below is an example of a simple HTML form. It has an action url of “hello.php” and a method of POST. As you can see, it also has two objects inside the form, a text box and a submit button. The text box is named “name”. This is an important tag as you will see later down the road. For now, let’s name this file index.php.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8″ />
<title>PHP: A Beginners Guide - Part 3</title>
</head><body>
<h1>Welcome to My Website</h1>
<p>Please enter your name below to continue.</p>
<form action=“hello.php” method=“post”>
<p>
<input type=“text” name=“name” />
<input type=“submit” name=“subForm” />
</p>
</form>
</body>
</html>
Now that we have a form requesting that our user enters in his/her name, we need something to process that form. This is where the file “hello.php” comes in. In PHP, you retrieve the value the user entered by using the “$_POST” command. To read more about the “$_POST” command, visit the PHP Manual. Our “hello.php” file looks like this:
<?php
$name = htmlspecialchars($_POST['name']);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8” />
<title>PHP: A Beginners Guide - Part 3</title>
</head><body>
<h1>Welcome, <?= $name ?></h1>
<p>We are so excited that you have decided to learn PHP with us! If you have any more questions, visit
<a href=“http://www.hunterjm.com”>hunterjm.com</a> for current tutorial updates!</p>
</body>
</html>
What we need to focus on here is the PHP at the top of the page. In PHP, the htmlspecialchars() function allows us to replace characters such as <, >, ‘, etc. with their counterparts (<, >, ‘). This is so people cannot inject HTML code into your web page.
The most important part of this script of course is the “$_POST”. In the above example, you see that post contains a string called “name” ( $_POST['name'] ). This is the name of the input box we defined in our “index.php” page earlier. What this command is doing is taking the value from that specific box, and echoing it onto the page after “Welcome, ” inside the h1 tag (remember the shortcut from yesterday’s tutorial?).
Passing Data with GET
Now that we have the user’s name, we may want to pass that, and other variables, to a different web page. This can be done by placing the information into the query string. The structure of a query string is simple. The first variable is stated by a question mark ( ? ) directly after the file name in the URL. The question mark is followed by the name of the variable, then an equal sign, finally followed by the value. If you want to pass more than one variable, simply place an ampersand ( & ) after the first variable. Let’s expand upon our “hello.php” and ask the user whether they prefer cats or dogs.
<?php
$name = htmlspecialchars($_POST['name']);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8″ />
<title>PHP: A Beginners Guide - Part 3</title>
</head><body>
<h1>Welcome, <?= $name ?></h1>
<p>Which animal do you like the most?</p>
<ul>
<li><a href=“pet.php?name=<?= $name ?>&pet=cat”>cat</a></li>
<li><a href=“pet.php?name=<?= $name ?>&pet=dog”>dog</a></li>
</ul>
<p>We are so excited that you have decided to learn PHP with us! If you have any more questions, visit
<a href=“http://www.hunterjm.com”>hunterjm.com</a> for current tutorial updates!</p>
</body>
</html>
As you can see, I have added an unordered list to the “hello.php” file. The text “cat” and “dog” are hyperlinks to the same page, but have different variables added to the end of them. The first variable is named “name”, and the value is the same as the value the user entered in the previous form (we are just writing the value out). The second value is named “pet” and the value changes depending on the link. If the link is for the cat then the value is “cat”, and vice versa. Lets now create “pet.php”.
<?php
$name = htmlspecialchars($_GET['name']);
$pet = htmlspecialchars($_GET['pet']);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8″ />
<title>PHP: A Beginners Guide - Part 3</title>
</head><body>
<h1><?= $name ?> is a <?= $pet ?> person!</h1>
<p>We are so excited that you have decided to learn PHP with us! If you have any more questions, visit
<a href=“http://www.hunterjm.com”>hunterjm.com</a> for current tutorial updates!</p>
</body>
</html>
The first thing you should notice is that we are still using htmlspecialchars() but we have replaced “$_POST” with “$_GET”. This tells it to look in the URL instead of the hidden POST message body. Other than that, we are still doing everything the same as in “hello.php” by echoing the values out onto the page.
Conclusion
We have gone over one of the most important topics in PHP today. Form values are used for almost everything you will want to do in PHP. Tomorrow, I will start going over “if” statements, which will allow you to chose what is to be done based on conditions which may or may not exist. Before you read tomorrow’s post, I highly recommend looking at the comparison operators.
Again, to download today’s tutorial click here. You may also view it live here.
No comments yet.