After submitting the form, PHP gets the implementation method of the submission content _php instance

Source: Internet
Author: User
Tags http post php form

question: After submitting a form on a Web page, why can't PHP get the content submitted? However, it is normal to run on the old version of PHP.

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

There are four solutions:

1. Modify php.ini, look for register_globals, and modify its value to on. This can be like the original, for example, the submitted form contains a variable named "username", then you can use the $username directly in PHP to access the variable. However, this method is not recommended unless you want to use a piece of old code that takes into account compatibility issues.

2. Use $HTTP _get_vars, $HTTP _post_vars array to access, for example, in the form of $http_post_vars["username". However, this method is not recommended.

3. (recommended) Use arrays such as $_post, $_get, etc. to access, for example, in the form of $_post["username". It is recommended that this approach be adopted.

(recommended) Use the Import_request_variables function. This function imports the commit content into the variable.

For example, Import_request_variables ("GP", "Rvar_"), the first parameter can select G,p,c, respectively, to import Get,post,cookie variables, and the second parameter is the variable prefix that is imported. After executing the above statement, you can use $rvar _username to access the submitted username variable. Use Import_request_variables ("GP", ""), and can be compatible with previous PHP programs.

PHP $_get and $_post variables are used to get information from a form, such as information entered by a user.

PHP form Action

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

Examples of forms:

The "welcome.php" file looks like this:

 
 

The script above will output the following result:

Welcome John.

You are are years old.

The PHP $_get and $_post variables will be explained in detail below.

form validation [form Validation]

The information entered by the user should be validated as much as possible through client-side scripting (e.g. JavaScript) browsers, and the validation of information through the browser can improve efficiency and reduce download pressure on the server.

If the user enters information that 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 a server is to send the form information to the current page for validation, not to other pages. With this method, if there is an error in the form, the user can get the error message directly on the current page. This makes it easier to find error messages that exist.

The PHP $_get variable is getting "value" from the form by a GET method.

$_get variable

The $_get variable is an array that contains the name [name] and values [value] (these names and values are sent through the HTTP GET method and can be exploited).

The $_get variable uses "Method=get" to get the form information. The information sent through the Get method is visible (it will appear in the browser's address bar), and it has a length limit (the total length of the information cannot exceed 100 characters [character]).

Case

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

The URL is displayed when the user clicks the Submit button

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

Welcome <?php echo $_get["name"] >.<br/>you are echo <?php "age"; $_get[?> years

Why use "$_get"?

Important: When you use the "$_get" variable, all variable names and variable values are displayed in the URL address bar, so when you send a message that contains a password or some other sensitive information, you can no longer use this method. Because all the information is displayed in the URL address bar, we can put it in the Favorites folder as a label. This is very useful in many cases.

Note: If you need to send a variable value that is too large, the HTTP get method does not apply. The amount of information sent cannot exceed 100 characters.

$_request variable

The PHP $_request variable contains the contents of $_get, $_post, and $_cookie.

The PHP $_request variable can be used to get form data sent by both the "get" and "POST" methods.

Case

Welcome <?php echo $_request["name"] >.<br/>you are echo <?php "age"; $_request[?> years

The function of the PHP $_post variable is to get the form variable sent by method = "POST".

$_post variable

The $_post variable is an array of [name] value [value] (these names and values are sent through the HTTP POST method and can be exploited)

The $_post variable uses "Method=post" to get the form information. The information sent through the Post method is not visible, and it does not have a limit on the length of the information.

Case

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

The URL does not contain any form data when the user clicks the Submit button

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

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

Why do you use $_post?

Variables sent over HTTP POST are not displayed in the URL

The size of the variable is not limited

However, because variables cannot be displayed in a URL, it is not possible to store the page as a label in a Favorites folder.

$_request variable

The PHP $_request variable contains the contents of $_get, $_post, and $_cookie

The PHP $_request variable can be used to get form data sent by both the "get" and "POST" methods.

Case

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

The above submission form after PHP to obtain the content of the implementation method is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.