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

PHP - Part 5

#3
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?f...eve&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

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