Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5

PHP - Part 5

#2
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.

Reply


Messages In This Thread
PHP - Part 5 - by Darth-Apple - August 19th, 2013 at 9:02 PM
RE: How to Program PHP Tutorials - Part 5 - by Darth-Apple - August 19th, 2013 at 9:58 PM
RE: How to Program PHP Tutorials - Part 5 - by Darth-Apple - August 19th, 2013 at 10:14 PM
RE: How to Program PHP Tutorials - Part 5 - by Damian B. - August 20th, 2013 at 12:46 PM
RE: How to Program PHP Tutorials - Part 5 - by Damian B. - August 28th, 2013 at 10:02 PM
RE: How to Program PHP Tutorials - Part 5 - by Darth-Apple - August 28th, 2013 at 10:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP - Part 6 Darth-Apple 2 6,256 November 12th, 2013 at 5:31 PM
Last Post: Damian B.
  PHP - Part 2 Darth-Apple 2 6,813 August 19th, 2013 at 9:26 PM
Last Post: Darth-Apple
  PHP - Part 1 Darth-Apple 4 9,711 August 19th, 2013 at 7:40 PM
Last Post: Damian B.
  PHP - Part 4 Darth-Apple 0 4,253 August 13th, 2013 at 4:29 PM
Last Post: Darth-Apple



Users browsing this thread: 1 Guest(s)

Dark/Light Theme Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2024
Forum design by Makestation Team © 2013-2024