August 19th, 2013 at 9:02 PM
How to Program PHP
Tutorial, Part 5
Tutorial, Part 5
Welcome to part 5 of the "how to program PHP" series! So far, we've explored a number of fundamental concepts, but we haven't really done anything interesting yet. In the last tutorial, we explored so called "conditional" statements, but we never really went anywhere with the concepts that were explained in part 4. In this tutorial, we will begin making stuff actually happen in PHP. If you do not have a server or localhost set up on your PC yet, I would very strongly recommend getting that set up now, as this tutorial, as well as the ones to follow, will be very helpful with a localhost. Remember, Windows users can simply install WAMP to get a localhost set up in a matter of minutes. So, from this point on, I will assume that you have a working server or localhost set up to work with.
In this tutorial, we will explore how to get HTML forms to work together with PHP to make something happen. I would recommend having a good understanding of HTML forms before beginning this tutorial. If you do not understand HTML forms, a simple google search yields hundreds of great resources to help you understand how HTML forms work. This tutorial will teach you how to code PHP scripts to work with HTML forms, and we'll soon be able to do some pretty neat things with that.
First, we need to go backwards a bit and set up the simple HTML form. I'm not going to do anything particularly complicated here. I'll just write a very simple HTML form, and we'll get started.
Code:
<html>
<head>
</head>
<body>
<form action="t5_demo.php" method="post">
First Name: <input type="text" name="firstname"><br>
Age: <input type="text" name="age"><br>
<input type="submit">
</form>
</body>
</html>
Paste the code above into a file called "t5_demo.html" (without the quotes) and save it into a folder on your server or localhost. As you may notice, we simply have an HTML form that asks for your first name and your age. There is nothing very complicated going on at this point. I encourage you to try this simple HTML page in your browser to get a feel for what is completed so far. You won't see much, but it is a valid form, and that is all we will need at this point.
Quote:Need help with HTML forms?
These tutorials won't attempt to review HTML, but if you need to brush up on your HTML scripts, this page from w3schools is a great reference!
How will we get this form to actually do anything? What is supposed to happen when "submit" is pressed? Well that's what the PHP side of this form is going to be for. Basically, the PHP is going to "process" this data and return something based on it. First, there are a few properties of this form that we should make note of. Look at the following line of code.
Code:
<form action="t5_demo.php" method="post">
Notice the action= field. This defines which file we will be sending the form data to for processing. We haven't actually created "t5_demo.php" yet, but we'll be creating that fairly soon.
Also, notice the "method=" property. Here, it is set to "post," as you can see in the code above. This property has two possible values. You can either use the "get" or the "post" method, but what is the difference?
The difference between "post" and "get" is that "get" sends data in the URL, and "post" does not. "get" is useful for defining page numbers, page IDs, and other things that you will need to assign a specific URL to. "POST" is good for passwords, etc... since, for obvious reasons, you don't want passwords showing up in the URL. If this sounds confusing, keep reading. We'll explain it in much more detail very shortly.
Anyway, this form still doesn't do anything. In order to make it functional, we need to write "t5_demo.php" to process the form's contents and we will then save it in the same folder that t5_demo.html is saved in. Remember that the "action=" property defines the URL to the PHP file that the form's data will be sent to. It is very important that the PHP file that processes the form has the same name that is found in the "action=" property of the form. Otherwise the browser won't be able to find the corresponding PHP file to send the form's data to.
We'll start with something very simple.
Code:
<?php
echo "hello, ".$_POST['firstname']."!";
?>
Save that into a file and call it "t5_demo.php". Make sure that it is in the same folder that your HTML file is in, and the name has to be exact. Then go to the HTML file's URL in your browser, and fill out the form. You should see something like this when you submit the form:
Quote:Hello, Steve!
This is an extremely simple script, spanning only three lines of code. What exactly does it do?
1) First, remember that "echo" is the command used to send data to the screen in your browser.
2) $_POST is an "array" that contains the values for all of fields in a form that uses "post" as its method. (See tutorial 2 for more information on arrays)
3) Notice how we used $_POST['firstname'] in the code. Look at the HTML file, and one of the <input> tags has a name attribute, and its assigned name is "firstname". In PHP, we refer to fields of a form by those assigned names. $_POST in fact contains the data from every field in a form, and you can refer to it easily. We'll explore more about $_POST in the next scripts that we will be making.
Anyway, as you can see, the script above is pretty simple, and simply is a "hello [yourname]" script. It doesn't do much that's actually interesting at this point, but it does show that PHP can be used to process form data. I'd recommend stopping and tweaking a few things at this point to get familiar with the concept. If it all seems very confusing, keep reading for more examples.