Makestation
PHP - Part 5 - Printable Version

+- Makestation (https://makestation.net)
+-- Forum: Technical Arts (https://makestation.net/forumdisplay.php?fid=45)
+--- Forum: Software (https://makestation.net/forumdisplay.php?fid=104)
+--- Thread: PHP - Part 5 (/showthread.php?tid=394)



PHP - Part 5 - Darth-Apple - August 19th, 2013

How to Program PHP
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. Big Grin


RE: How to Program PHP Tutorials - Part 5 - Darth-Apple - August 19th, 2013

How to Program PHP
Tutorial - part 5.1

So... we very briefly introduced the concept of using PHP to process HTML form data, but we never really did anything interesting with it. Turns out there is quite a lot that can be done with this simple concept, so this "tutorial" will just extend the previous post and show what more can be done.

We'll be using the same HTML file as before, so we'll be using the same form. Just to refresh...

Code:
<html>
<head>
</head>
<body>
<form action="tE5_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>

Our PHP file will look a bit different, however.

Code:
<?php

echo "hello, ".$_POST['firstname']."!<br>";
if ($_POST['age'] > 12) {
echo "Congratulations! You are over 13 years of age, and will be allowed to register. ";
}
else {
echo "Sorry, but users under the age of 13 are not allowed to register. ";
}

?>

Save that into the same PHP file that you used before. Make sure that it has the same filename as was used before, or else the form won't work.

Either way, this form is now more functional. It makes use of a concept that we learned in the previous tutorial with the conditional statement. As you can see, it checks to see if your age is at least 13. If your age is 13 or above, it tells you that you are eligible for registration. If you are under 13, it tells you that you aren't eligible. This is also a very simple script, but it demonstrates what can be done. This is also quite applicable in the real world, as in the United States, COPPA laws prohibits children under 13 from submitting registrations for most websites.

Here is another example:

In your t5_demo.html file, replace the contents with:

Code:
<html>
<head>
</head>
<body>
<form action="t5_demo.php" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Confirm Password: <input type="password" name="password_conf"><br>

<input type="submit">
</form>

</body>
</html>

And in your t5_demo.php file, replace the contents with:

Code:
<?php

echo "hello, ".$_POST['username']."! Your registration has been received by the system!<br>";
$password = $_POST['password'];
$password_confirm = $_POST['password_conf'];

if ($password == $password_confirm) {
echo "Congratulations! Your passwords match!<br>";
}
else {
echo "sorry, but the passwords you entered do not match. Please try again.";
}

?>

Then save both files and visit the form in your browser. Notice how it is now a very simple "registration" form. The form, when submitted, will check to see if your passwords match. I recommend playing around with it to see how the script works as it validates your input. As you can see, a lot is possible with PHP form validation.


RE: How to Program PHP Tutorials - Part 5 - Darth-Apple - August 19th, 2013

How to Program PHP
Tutorial, part 5.2

So far, we've used only "post" as the method for submitting forms. What would happen if you were to use "get" instead? Well, as said in the first post of this tutorial, "get" sends the data in the URL, while "post" does not. "post" also uses the $_POST variable in PHP, whereas "get" uses the $_GET variable. The differences, however, weren't explained in very much detail. This post will explain the difference in more detail and will show what would happen if we were to use "get" instead.

Quote:Pro Tip: Beware of the passwords...

In some cases, it does not really matter whether you choose to use "get" or "post" for your forms. When dealing with passwords, it is very important that you always use "post" however. The reason is because otherwise the passwords would get sent to the PHP script via the URL, which makes it plainly visible in the URL bar of the browser. This is a very bad idea, for obvious reasons.

So, just to go back to the original tutorial, what would it have looked like if we had used "get" instead of "post"?

In your t5_demo.html file, paste the following code, then save the file on your localhost.

Code:
<html>
<head>
</head>
<body>
<form action="t5_demo.php" method="get">
First Name: <input type="text" name="firstname"><br>
Age: <input type="text" name="age"><br>
<input type="submit">
</form>
</body>
</html>

In your t5_demo.php file, paste the following code, then save the file in the same folder that your HTML file is in.

Code:
<?php

echo "hello, ".$_GET['firstname']."!<br>";
if ($_GET['age'] > 12) {
echo "Congratulations! You are over 13 years of age, and will be allowed to register. ";
}
else {
echo "Sorry, but users under the age of 13 are not allowed to register. ";
}

?>

The code functions almost exactly like the version that uses "post" functions, but you will notice two small differences.

1) Notice how, in our PHP, $_GET is used instead of $_POST.
2) Notice the URL bar... it has something like this in it:

Quote:http://localhost/tutorials/t5_demo.php?firstname=Steve&age=18

Notice how your input for the form shows up in the URL bar. The same version of our script that uses "post" instead did not have anything show up in the URL bar. This is the fundamental difference between get and post.

In future tutorials, we will be using the "get" and "post" methods interchangeably, depending on the needs of the script. If this all seems confusing to you, bear with me in the future tutorials, and the differences in both use and application will become more clear as we do more advanced things.

In the meantime, feel free to play around with your own scripts and design HTML forms with PHP to validate or "process" them! Big Grin

Next Tutorial: Introduction to Loops
Directory of PHP Tutorials


RE: How to Program PHP Tutorials - Part 5 - Damian B. - August 20th, 2013

Great tutorials! I can't wait for part 6. Smile


RE: How to Program PHP Tutorials - Part 5 - Damian B. - August 28th, 2013

Any news on when part 6 will come?


RE: How to Program PHP Tutorials - Part 5 - Darth-Apple - August 28th, 2013

I'd like to get it written within a couple of days. I have been very busy lately, and am also coming down with a virus, so it has been hard to find the time to write them.

I should have something written up pretty soon though. Big Grin

Edit: Part 6 is here: http://makestation.net/thread-743.html