Beginners PHP 1:PHP GET
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.
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();
}
?>
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
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 

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.

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();
}
?>
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
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...
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
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
//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>
<?php
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
I Cannot Seem to get this working to be honest,
The video part doesnt work it shows the text and does nothing.
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 

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...
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();
}
?>
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
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 

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 

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();
}
?>
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
Copyright Information
Copyright © Codenstuff.com 2020 - 2023