tags:

views:

285

answers:

11

I know it is php global variable but I'm not sure, what it do? I also read from official php site, but did not understand.

A: 

It contains any values posted from a HTML form to this script.

Jeremy Smyth
you mean, what ever we post from html, it will hold those values??
Syed Tayyab Ali
This is only true when a form with a POST type is used. Really, HTML forms are just abstractions on top of HTTP POST requests, which is where the data in $_POST actually comes from.
Peter Bailey
anything from inputs contained within a <form action='yourscript.php' method='POST'> form :)
Jeremy Smyth
+1  A: 

It's used to store CGI input via a POST sent to your page.

Example:


Your page contains:

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> 


One the user submits the values input into the form, you can access those variables through $_POST using the names you provided for the input tags.

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

RC
+5  A: 

You may want to read up on the basics of PHP. Try reading some starter tutorials.

$_POST is a variable used to grab data sent through a web form.

Here's a simple page describing $_POST and how to use it from W3Schools: PHP $_POST Function

Basically:

Use HTML like this on your first page:

<form action="submit.php" method="post">
  Email: <input type="text" name="emailaddress" /> <input type="submit" value="Subscribe" />
</form>

Then on submit.php use something like this:

<?
  echo "You subscribed with the email address:";
  echo $_POST['emailaddress'];
?>
T Pops
It is also array.
Syed Tayyab Ali
Absolutely. The information sent through post data is sorted into the $_POST superglobal variable as an array indexed by the name of the input element on the form.
T Pops
@Sayed, yes, it is an associate array, similar to a hash in other languages.http://www.w3schools.com/php/php_arrays.asp
Josh
+1  A: 

$_POST is used to retreive values passed to your page via a POST request.

For example, your page uses a form to pass data to another page in your application. Your form would have

<form method="post">

to pass those values via POST.

It is matched by $_GET which perform the same funtion for GET requests.

If you want to be able to reference either GET/POST values, you can use $_REQUEST

Justin Niessner
+1  A: 

You can capture post values from forms:

Example:

<form method="POST">
    <input type="text" name="txtName" value="Test" />
</form>

To get this you'll use:

$_POST["txtName"];
Zanoni
+1  A: 

It contains data sent by HTTP post, this is most often from a HTML FORM.

<form action="page.php" method="post">
<input type="text" name="email" ...>
...
</form>

Will be accessible by

$_POST["email"]
Kimble
+1  A: 

It contains the data submitted via the POST method, and only the POST method, versus data submitted via the GET method. The $_REQUEST superglobal variable contains both $_POST and $_GET data.

Andrew Sledge
You mean, if form is posted by using post method, all the variable within that form are also accessible through $_REQUEST['variable']
Syed Tayyab Ali
The POST data is also accessible from $_REQUEST. I don't use this myself, but I've seen it used to make the form method types interchangeable.
Andrew Sledge
+1  A: 

When data is posted through a form to the server, you access it through the $_POST array:

<form method="post">
  <p><input type="text" name="firstname" /></p>
  <p><input type="submit" /></p>
</form>

--

<?php

  if ($_POST)
    print $_POST["name"];

?>

Not all data is sent through $_POST through. File uploads are done through $_FILES.

Jonathan Sampson
+4  A: 

There are generally 2 ways of sending an HTTP request to a server:

  • GET
  • POST

Say you have a <form> on a page.

<form method="post">
  <input type="text" name="yourName" />
  <input type="submit" />
</form>

Notice the "method" attribute of the form is set to "post". So in the PHP script that receives this HTTP request, $_POST[ 'yourName' ] will have the value when this form is submitted.

If you had used the GET method in your form:

<form method="get">
  <input type="text" name="yourName" />
  <input type="submit" />
</form>

Then $_GET['yourName'] will have the value sent in by the form.

$_REQUEST['yourName'] contains all the variables that were posted, whether they were sent by GET or POST.

bobobobo
it make sense to me now.
Syed Tayyab Ali
+1  A: 

As defined by the Hypertext Transfer Protocol specifications, there are several types of requests that a client (web browser) can make to a resource (web server).

The two most common types of web requests are GET and POST. PHP automatically loads any client request data into the global arrays, $_GET and $_POST, based on the type of web request received. The type of request is transparent to the user of the web browser, and is simply based on what is going on in the page. In general however, any regular link you click produces a GET request, and any form you submit produces as POST request.

If you click a link that goes to "http://example.com/index.php?x=123&amp;y=789", then index.php will have it's $_GET array populated with $_GET['x'] = '123' and $_GET['y'] = '789'.

If you submit a form that has the following structure:

<form action="http://example.com/index.php" method="post">
<input type="text" name="x">
</form>

Then the receiving script, index.php, will have it's $_POST array populated with $_POST['x'] = 'whatever you typed into the textbox named x';

zombat
+1  A: 

There are two ways of sending data from a form to a web app, GET and POST.

GET sends the data as part of the URL string: http://www.example.com/get.html?fred=1&amp;sam=2 is an example of what that would look like. There are some problems with using it for all processing, one of the biggest is that every browser has a different maximum length for the query string, so you may have your data truncated.

POST sends them separately from the URL. You avoid the short length limit, plus you can send binary or encrypted data with POST.

In the first example above, PHP can retrieve the values sent by $_GET['fred'] and $_GET['sam']. You would use $_POST instead if the form was POSTed.

If you're wondering which method you should use, start here

Arlen