November 8th, 2013 at 2:34 AM
How to Program PHP
Tutorial, Part 6.1
Tutorial, Part 6.1
I am now going to make a new "interactive" counter script to add on to this simple tutorial. This will create a very simple form, and once this form is submitted, it will count up to the value that you have entered. Save this to a file named counter.php, and upload it to your server and give it a try!
Code:
<?php
// this script creates a counter
if (isset($_POST['submitted'])) {
// a form has been submitted, so lets start counting!
if ((int)$_POST['max'] != null) {
// checks to see if a number was actually entered.
$count = 0;
while ($count < ($_POST['max'] + 1))
{
echo $count. "<br>";
$count = $count + 1;
}
}
else {
// a number wasn't entered, so we can't count.
echo "you didn't enter a number!";
}
}
else {
// no form was submitted, so we will need to generate the form.
echo "
<form action='' method='post'>
<input type='hidden' name='submitted' value='1'>
Count to: <input type='text' name='max' value='100'> <br />
<input type='submit'>
</form>
";
}
?>
This script makes use of a while loop, and also makes use of a few IF statements to validate the data. As you may also notice, for the first time in my tutorials, I have only made use of one file, despite having both an HTML form and PHP. The HTML form is actually in this PHP file, and this script uses IF statements to allow it to auto-detect whether a form has been submitted.
In the past, I've used separate files for HTML forms to separate them from the PHP for simplicity purposes, but this isn't actually a particularly common practice in many real world applications, largely because it can be inconvenient to create a separate HTML file for every single HTML form on a large application. I felt like this was a good time to go ahead and explain how this is done, and most of my future tutorials will probably do something similar down the road. Anyway, back to explaining...
Notice this line near the top of the script:
Quote:if (isset($_POST['submitted'])) {
This line basically checks to see if a field named "submitted" was set. As you may notice, we're using the isset() function for that purpose. This is probably a function you will see being used in quite a lot of real world scripts.
Also, notice the following line in our script:
Quote:<input type='hidden' name='submitted' value='1'>
Notice that we set its type to be "hidden". Just because it is hidden doesn't mean it can't have a value. It has a default value of 1, so it is always set unless the user decides to tamper with the browser's webmaster tools. Unless someone is trying to cheat our little system, this field in our form will ALWAYS be set to 1 if the form is submitted, even though you can't actually see the field, since it is set to be hidden.
Anyway, because this field will always be set if the form is submitted, we will use it to serve as a marker for this form that shows the script that it has actually been submitted. When we check with the IF statement to see if it is set, we are actually checking to see if the user submitted a form. If a form was submitted and this marker is set, the script will attempt to process this form. If, however, this marker is not set, the script will know that no form was submitted, so it will generate the form instead.
The script also validates with another IF statement to make sure that a number was actually submitted. If the user leaves the form blank, the form may be submitted and may have the "submitted" marker, but it doesn't have any data to process. Turns out you'll have to do quite a lot of meticulous validation of user input in real world applications, especially when databases are involved. It is one of the most important principles of application security never to trust user input blindly, so we'll be seeing a lot of this in future tutorials.
Also, notice the following line
Code:
<form action='' method='post'>
In the past tutorials, we haven't left the "action" field blank. However, because we are sending the form to the same script that generated the form, we don't have to put anything in this field. Remember, the script can auto-detect whether the form was submitted, so this is a perfectly acceptable practice.
Other than that, this script is pretty simple. It makes use of the while statement, and also makes use of IF statements. I would recommend that you play around with this script on your own to see if you can get a feel for how it works. We'll be using concepts like these quite frequently in future tutorials. Stay tuned!