Beginners PHP 1:PHP GET

15 posts Page 1 of 2
Contributors
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Beginners PHP 1:PHP GET
Alex
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:
Code: Select all
<?php
/*index.php*/
$message = $_GET['message'];
echo 'You said "'.$message.'"';
?>
Now I'll break it down.
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
You said "awesome"
if I made the url "index.php?message=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
<?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>
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.
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
<?php
//add_complete.php
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$result = $num1 + $num2;

//echo result
echo $result;
?>
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.

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.
Some notes:
  • 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.
Thank you,
Alex.
You do not have the required permissions to view the files attached to this post.
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
User avatar
Bogoh67
VIP - Site Partner
VIP - Site Partner
Posts: 656
Joined: Sun Apr 18, 2010 8:20 pm

Re: Beginners PHP 1:PHP GET
Bogoh67
thats very cool but seriously maybe you should start making more tutorials i see you only have 18 posts and i like you tuts so make more, more often ok :)
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Re: Beginners PHP 1:PHP GET
Alex
Thanks. I have plans on making a new project in PHP. A tutorial website where users can create and search for tutorials. I'll start working on the POST tutorial. Thanks for your feedback. :)

Edit: Maybe I'll even get into some Object Oriented PHP.
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
User avatar
Livengood
Serious Programmer
Serious Programmer
Posts: 445
Joined: Tue Feb 16, 2010 6:24 am

Re: Beginners PHP 1:PHP GET
Livengood
now lets say you have a video player, but you want to use this to videos.? how would you do this, i know some php, but i am lost on trying to do this...
Image
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Re: Beginners PHP 1:PHP GET
Alex
Livengood wrote:
now lets say you have a video player, but you want to use this to videos.? how would you do this, i know some php, but i am lost on trying to do this...
Well, you could get the video id and have in your root web directory a folder named "videos" so when a user uploads a video, it uploads it to that folder by the name of "1.avi","2.avi" and so on. Now, to use $_GET[] for that, it'd be something along the lines of...
Code: Select all
<?php
//playvideo.php (Or whatever you want to call it)
$videoid = $_GET['id'];
?>
<html>
<head>
<TITLE> Show Video</TITLE>
</head>
<body>
<embed src="<?php echo 'http://www.yourwebsite.com/videos/'.$videoid.'.avi'; ?>" autostart="false" />
</body>
</html>
Basically, we get the id of the video from the URL (http://www.yourwebsite.com/playvideo.php?id=1) and it would embed the video "http://www.yourwebsite.com/videos/1.avi" and tell it not to start on page load.
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Re: Beginners PHP 1:PHP GET
zachman61
I Cannot Seem to get this working to be honest,
The video part doesnt work it shows the text and does nothing.
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Re: Beginners PHP 1:PHP GET
Alex
I got it to work by changing the extension .avi to .wmv

I've even prepared a package for you.
You'll need to supply your own videos though, so the space of the package doesn't end up being 500mb or something...
You do not have the required permissions to view the files attached to this post.
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Re: Beginners PHP 1:PHP GET
zachman61
thanks ill run through it real fast right now :)
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Re: Beginners PHP 1:PHP GET
zachman61
thanks ill run through it real fast right now :)
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Re: Beginners PHP 1:PHP GET
Alex
Also guys, for projects I suggest you use Wamp Server. It's light-weigt and works VERY well. Google it.
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
15 posts Page 1 of 2
Return to “Tutorials”