Home  >  Article  >  Backend Development  >  How to obtain the submitted content in PHP after submitting the form, form php_PHP tutorial

How to obtain the submitted content in PHP after submitting the form, form php_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:50:391009browse

How to implement PHP to obtain the submitted content after submitting the form, form php

Question: After submitting the form on the web page, why does PHP Can't get the submitted content? However, it runs normally on older versions of PHP.

The new version of PHP has abandoned the original form content processing method, that is, it no longer directly copies the content of the submitted form into a variable with the same name.

There are four solutions:

1. Modify php.ini, find register_globals, and change its value to On. In this way, it can be the same as before. For example, if the submitted form includes a variable named "username", then you can directly use $username to access the variable in PHP. However, this approach is not recommended unless you are using an older piece of code and have compatibility issues.

2. Use the $HTTP_GET_VARS and $HTTP_POST_VARS arrays to access, for example, write it in the form of $HTTP_POST_VARS["username"]. However, this method is not recommended.

3. (Recommended) Use $_POST, $_GET and other arrays to access, for example, write in the form of $_POST["username"]. This approach is recommended.

(Recommended) Use the import_request_variables function. This function imports the submission content into a variable.

For example, import_request_variables("gp", "rvar_"); the first parameter can choose g, p, c, which means importing GET, POST, COOKIE variables respectively; the second parameter is the imported variable prefix. After executing the above statement, you can use $rvar_username to access the submitted username variable. Use import_request_variables("gp", ""); to be compatible with previous PHP programs.

PHP $_GET and $_POST variables are used to obtain information in the form, such as information entered by the user.

PHP form operation

When we deal with HTML forms and PHP forms, the important thing to remember is that any form element in the HTML page can be automatically used in PHP scripts:

Form example:

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

The above HTML page contains two input fields and a submit button. When the user fills out the information and clicks the submit button, the form data will be sent to the "welcome.php" file.

The "welcome.php" file looks like this:

<html>
<body>Welcome <&#63;php echo $_POST["name"]; &#63;>.<br />
You are <&#63;php echo $_POST["age"]; &#63;> years old.</body>
</html> 

The above script will output the following result:

Welcome John.

You are 28 years old.

PHP $_GET and $_POST variables will be explained in detail below.

Form Validation[Form Validation]

The information entered by the user should be verified through the client script program (such as JavaScript) on the browser as much as possible; verifying the validity of the information through the browser can improve efficiency and reduce the download pressure on the server.

If the information entered by the user needs to be stored in the database, then you must consider validation on the server side. The best way to verify the validity of information on the server is to send the form information to the current page for verification instead of transferring it to other pages for verification. Through the above method, if there is an error in the form, the user can directly obtain the error information on the current page. This makes it easier for us to detect the presence of error messages.

The PHP $_GET variable obtains the "value" from the form through the get method.

$_GET variable

The $_GET variable is an array containing name [name] and value [value] (these names and values ​​are sent via the HTTP GET method and can be exploited).

The $_GET variable uses "method=get" to obtain form information. The message sent via the GET method is visible (it will be displayed in the browser's address bar), and it has a length limit (the total length of the message cannot exceed 100 characters).

Case

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

When the user clicks the "Submit" button, the URL is displayed

The "welcome.php" file can use the "$_GET" variable to obtain form data (note: the name in the form field [form field] will automatically be used as the ID keyword in the "$_GET" array):

Welcome 19e28891b9e10b25bdae399f33b35e5c.df250b2156c434f3390392d09b1c9563You are 4ab30ab8d9dcba8407ecc2c6b264d3f1 years old!

Why use "$_GET"?

Important points: When using the "$_GET" variable, all variable names and variable values ​​will be displayed in the URL address bar; therefore, when the information you send contains passwords or other sensitive information, it can no longer be used. This method. Because all information will be displayed in the URL address bar, we can put it as a label in favorites. This is very useful in many situations.

Note: If the variable value to be sent is too large, the HTTP GET method will not be applicable. The amount of information sent cannot exceed 100 characters.

$_REQUEST variable

PHP $_REQUEST variable contains the contents of $_GET, $_POST, and $_COOKIE.

PHP $_REQUEST变量可以用来获取通过“GET”和“POST”这两种方法发送的表单数据。

案例

Welcome e2a972399fd96afef5b1c7e69fec4df1.df250b2156c434f3390392d09b1c9563You are de5308603027cd7d8415814e083d6cab years old!

PHP $_POST变量的作用是:获取method = “post”方法发送的表单变量。

$_POST变量

$_POST变量是一个包含名称[name]何值[value]的数组(这些名称和值是通过HTTP POST方法发送的,且都可以利用)

$_POST变量使用“method=POST”来获取表单信息。通过POST方法发送的信息是不可见的,并且它没有关于信息长度的限制。

案例

<form action="welcome.php" method="post">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form>

当用户点击“提交Submit”按钮后,URL中不会包含任何表单数据

“welcome.php”文件可以使用“$_POST”变量来获取表单数据(注意:表单栏[form field]内的名称将会自动作为“$_POST”数组中的ID关键词):

Welcome <&#63;php echo $_POST["name"]; &#63;>.<br />
You are <&#63;php echo $_POST["age"]; &#63;> years old!

为什么要使用$_POST?

通过HTTP POST发送的变量不会在URL中显示出来

变量的大小没有限制

然而,因为变量不能在URL中显示出来,所以也不可能把这个页面作为标签储存在收藏夹里。

$_REQUEST变量

PHP $_REQUEST变量包含$_GET, $_POST, and $_COOKIE的内容

PHP $_REQUEST变量可以用来获取通过“GET”和“POST”这两种方法发送的表单数据。

案例

 

Welcome <&#63;php echo $_REQUEST["name"]; &#63;>.<br />
You are <&#63;php echo $_REQUEST["age"]; &#63;> years old!

以上这篇提交表单后 PHP获取提交内容的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持帮客之家。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133088.htmlTechArticle提交表单后 PHP获取提交内容的实现方法,表单php 问题: 网页上提交表单之后,PHP为什么不能获取提交的内容?然而在老版本的PHP上运行却...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn