What is the purpose of $_POST?

ぃ、小莉子 提交于 2019-12-20 04:56:06

问题


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.


回答1:


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'];
?>



回答2:


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.




回答3:


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.




回答4:


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"];



回答5:


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"]



回答6:


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.




回答7:


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.




回答8:


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&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';




回答9:


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&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




回答10:


$_POST is used to retrieve 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 function for GET requests.

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




回答11:


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



来源:https://stackoverflow.com/questions/1039797/what-is-the-purpose-of-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!