Beginners PHP 1:PHP GET
Posted: Mon Nov 15, 2010 11:13 pm
In PHP, the GET function is a variable set through the URI. (i.e http://www.yoursite.com/index.php?variable1=hello)
A perfect example of this is search engines. When you use the search box at Google and submit your search, if you notice the URL you'll see something like "http://google.com/search?q=awesome". q is a variable that the website reads and is used to submit database querys to get your search results, but I'm not going to get into that for now. Also, if this were a directory containing index.php, it would be /search/?q=awesome, but Google uses some .htaccess ModRewriting. That can be explained later.
Lets start with a simple message generator. Lets call this file index.php:
First, we set a variable, $message, equal to $_GET['message']. The $_GET[$var] function gets the string variable you entered for $var and returns it in text. Then we echo You said " then we display the variable $message and end it with a quotation. The outcome would look like:
Lets make a simple addition script, "add.php". What we'll do is ask the user to enter two numbers in a form, then submit the form into a script "add_complete.php" using the form method "GET"
Here's the code, we'll break it down next.
"add.php":
We create 3 fields.
Number 1. Name="num1" and we set value to nothing.
Number 2. Name="num2" and we set value to nothing.
Submit Button. We set value to "Add Numbers!". That's the text that will display on the button.
Now for add_complete.php:
Conclusion:
Alex.
A perfect example of this is search engines. When you use the search box at Google and submit your search, if you notice the URL you'll see something like "http://google.com/search?q=awesome". q is a variable that the website reads and is used to submit database querys to get your search results, but I'm not going to get into that for now. Also, if this were a directory containing index.php, it would be /search/?q=awesome, but Google uses some .htaccess ModRewriting. That can be explained later.
Lets start with a simple message generator. Lets call this file index.php:
Code: Select all
Now I'll break it down. <?php
/*index.php*/
$message = $_GET['message'];
echo 'You said "'.$message.'"';
?>
First, we set a variable, $message, equal to $_GET['message']. The $_GET[$var] function gets the string variable you entered for $var and returns it in text. Then we echo You said " then we display the variable $message and end it with a quotation. The outcome would look like:
Code: Select all
if I made the url "index.php?message=awesome"You said "awesome"
Lets make a simple addition script, "add.php". What we'll do is ask the user to enter two numbers in a form, then submit the form into a script "add_complete.php" using the form method "GET"
Here's the code, we'll break it down next.
"add.php":
Code: Select all
First, we start HTML, the HEAD and add a TITLE. Then, we create a form by the name of form1, and we set the action, or where it submits to, to "add_complete.php" and the method is GET. You should understand what that is by now. If you don't go back and re-read.<?php
//add.php : page with the form to submit.
//by Alex on code 'n' stuff.
?>
<html>
<head>
<TITLE>Add Numbers</TITLE>
</head>
<body>
<form name="form1" action="add_complete.php" method="GET">
<p>Number 1: <input type="text" name="num1" value="" /></p>
<p>Number 2: <input type="text" name="num2" value="" /></p>
<p><input type="submit" value="Add Numbers!" /></p>
</form>
</body>
</html>
We create 3 fields.
Number 1. Name="num1" and we set value to nothing.
Number 2. Name="num2" and we set value to nothing.
Submit Button. We set value to "Add Numbers!". That's the text that will display on the button.
Now for add_complete.php:
Code: Select all
Here we get the value out of the url of num1. We set that to $num1. We then set the value from the url of num2 to $num2. Then we create a variable $result and set it to $num1 + $num2 and echo $result. As you get more advanced, you can filter the $num variables to make sure they're only numeric values.<?php
//add_complete.php
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$result = $num1 + $num2;
//echo result
echo $result;
?>
Conclusion:
- $_GET[]; gets the variable out of the URL
- Forms have a GET method which we can submit to another script.
- In my next tutorial I will go over the other method, POST.
- Please do not comment on errors due to entering "hello" + "1" or something like that.
- Feel free to ask questions. Please PM me though so we don't spam the comments.
- If you see an error in my tutorial, feel free to point it out to me.
- CONSTRUCTIVE Criticism only. This is my first tutorial, like ever.
Alex.