Forum Topic: Php: Basic Superglobals

(1,466 views • 8 replies)

This topic is 1 page long.

<< < > >>
None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/18/06 03:40 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Php: Main - All your PHP needs!

In this tutorial, you will be learning about three superglobal arrays:

$_POST
$_GET
$_REQUEST

This will be a some-what short tutorial, but these are all very important and should be taught nonetheless. So lets begin with the $_POST suberglobal.
-----------
$_POST

The $_POST superglobal is used to access information submitted by forms via the HTTP POST method. The $_POST array is ideal to use when wanting to submit large amounts of information, because there is no limit with it. Basically, it works "inivsibly" without the user seeing what was submitted. An example is below.

A basic form:

<form action="test.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>

We place the method="post" property in the <form> tag so we can let the form know we wish to send data via post. When the user clicks the submit button, they will be taken to a page like this:

somesite.com/test.php

On test.php, we can recieve the data the user submitted by using the $_POST array like this:

<?php
$fname=$_POST['fname'];
$lname=$_POST['lname'];
echo "Hello $fname $lname.";
?>

We set two variables, $fname and $lname to the values of the submitted data. This is possible because since the data was submitted via post, we can access it via post.

The alternate form of $_POST is $HTTP_POST_VARS. It is like $_POST, but is not superglobal. To make it so, you must add global before it.
-----------
$_GET

The $_GET superglobal is used to access information submitted by forms via the HTTP GET method. The $_GET array is ideal to use when wanting to submit short amounts of information. Since the values are located in the url, pages can be bookmarked with the data in the url so it can be used over and over again. The get method is used by putting the data at the end of the url of a page. It is usually done by having a page like *.php?var=blah&somevar=blah. Note that you should never use thye get method when submitting sensitvie data, and that string values in the url do not need quotes. Examples of $_GET are below.

A basic form:

<form action="test.php" method="get">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>

We place the method="get" property in the <form> tag so we can let the form know we wish to send data via get. When the user clicks the submit button, they will be taken to a page like this:

somesite.com/test.php?fname=somedata&lname
=somedata

On test.php, we can recieve the data the user submitted by using the $_GET array like this:

<?php
$fname=$_GET['fname'];
$lname=$_GET['lname'];
echo "Hello $fname $lname.";
?>

We set two variables, $fname and $lname to the values of the submitted data. This is possible because since the data was submitted via get, we can access it via get. Also note that when separating variables in the url, use ampersands ( & ).

The alternate form of $_GET is $HTTP_GET_VARS. It is like $_GET, but is not superglobal. To make it so, you must add global before it.
-----------
$_REQUEST

The $_REQUEST method is used to retrieve data submitted via get, or post. It can also handle $_COOKIE data. I prefer not to use the $_REQUEST superglobal because the user could submit the data however he/she wants out of these three ways. But, if you wish to use it, I will explain and give examples.

A basic form:

<form action="test.php">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>

Since we are using the request method, we do not need to specify how to submit the data. We can just totally get rid of the method property. When the user presses the submit button, they will be taken to a page like this:

somesite.com/test.php

On test.php, we can recieve the data the user submitted by using the $_REQUEST array like this:

<?php
$fname=$_REQUEST['fname'];
$lname=$_REQUEST['lname'];
echo "Hello $fname $lname.";
?>

We set two variables, $fname and $lname to the values of the submitted data. This is possible because since the data was submitted via anyway, we can access it via $_REQUEST. So, I reccomend using either $_POST or $_GET when recieving data from forms, but use $_REQUEST if you wish.
-----------
The tutorial is now over. I hope you learned a little something today :)

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 7/18/06 03:45 PM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,284

I think this tutorial would help a beginner very well. I like it :)


None

NinoGrounds

Reply To Post Reply & Quote

Posted at: 7/18/06 05:58 PM

NinoGrounds EVIL LEVEL 17

Sign-Up: 11/28/05

Posts: 3,756

It's good for begginers!

Nice.


None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 7/18/06 09:47 PM

Pilot-Doofy NEUTRAL LEVEL 37

Sign-Up: 09/13/03

Posts: 12,275

I think dynamic superglobals should be a bigger part of this tutorial. Perhaps include some coverage on the $_SERVER superglobal. That's a big one that can be very useful.


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/18/06 11:21 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/18/06 09:47 PM, Pilot-Doofy wrote: I think dynamic superglobals should be a bigger part of this tutorial. Perhaps include some coverage on the $_SERVER superglobal. That's a big one that can be very useful.

I know, but is $_SERVER really basic? I may make an advanced superglobals tutorial, unless someone else wants to.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Cryzabey

Reply To Post Reply & Quote

Posted at: 7/23/06 12:29 AM

Cryzabey LIGHT LEVEL 11

Sign-Up: 06/27/06

Posts: 686

I understand it but is there a way to send data to a .txt or .cos file (or create one) and then access it later using this?

BBS Signature

None

DFox

Reply To Post Reply & Quote

Posted at: 7/23/06 12:31 AM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,463

At 7/23/06 12:29 AM, Cryzabey wrote: I understand it but is there a way to send data to a .txt or .cos file (or create one) and then access it later using this?

Well you can read and write files with PHP... Check out this tutorial: http://www.newground../topic.php?id=396393


None

Tigiot

Reply To Post Reply & Quote

Posted at: 7/23/06 12:34 AM

Tigiot NEUTRAL LEVEL 03

Sign-Up: 07/22/06

Posts: 23

I have a tutorial on this subject written on another programming forum, too bad I didn't think of posting it.


Happy

zabar

Reply To Post Reply & Quote

Posted at: 9/9/06 05:44 AM

zabar DARK LEVEL 04

Sign-Up: 08/30/06

Posts: 8

Very helpfull, i found that i am learing PHP from NG BBS. Lol
But thx again, i didnt knew that!


All times are Eastern Standard Time (GMT -5) | Current Time: 05:32 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!