After submitting a form, PHP will obtain the implementation method of the submitted content. the php_PHP form tutorial

Source: Internet
Author: User
Tags php form
After the form is submitted, PHP obtains the implementation method of the submitted content. The form is php. PHP implements the method for obtaining submitted content after submitting a form. php has the following question: why cannot PHP obtain submitted content after submitting a form on a webpage? However, in the old version of PHP, the implementation method of PHP to obtain the submitted content after the form is submitted is run, and the form php

Problem:Why can't PHP obtain the submitted content after submitting a form on the webpage? However, it runs normally in the old version of PHP.

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

There are four solutions:

1. modify php. ini, search for register_globals, and change its value to On. In this way, the variable named "username" is included in the submitted Form. for example, you can directly use $ username to access the variable in php. However, you are not recommended to use this method unless you want to use an old piece of code with compatibility considerations.

2. use the $ HTTP_GET_VARS and $ HTTP_POST_VARS arrays for access. for example, use the $ HTTP_POST_VARS ["username"] format. However, this method is not recommended.

3. (recommended) use $ _ POST, $ _ GET and other arrays for access, for example, in the form of $ _ POST ["username. This method is recommended.

(Recommended) use the import_request_variables function. This function imports the submitted content into the variable.

For example, import_request_variables ("gp", "rvar _"); you can select g, p, and c as the first parameter to import GET, POST, and COOKIE variables; the second parameter is the prefix of the imported variable. After executing the preceding 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 user input information.

PHP form Operations

When processing HTML and PHP forms, we must remember that any form element in the HTML page can be automatically used for PHP scripts:

Form example:

 

The preceding HTML page contains two input boxes [input field] and a [submit] button. When you fill in the information and click the submit button, the data in the form will be sent to the "welcome. php" file.

The "welcome. php" file is as follows:

Welcome <?php echo $_POST["name"]; ?>.
You are <?php echo $_POST["age"]; ?> years old.

The above script will output the following result:

Welcome John.

You are 28 years old.

The PHP $ _ GET and $ _ POST variables are described below.

Form validity verification [Form Validation]

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

If the information you enter needs to be stored in the database, you must consider verifying the validity on the server side. The best way to verify the information validity on the server is to send the form information to the current page for verification, rather than transfer it to other pages for verification. If an error occurs in the form, you can obtain the error information directly on the current page. This makes it easier for us to find 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 the name [name] and value [value] (these names and values are sent through the http get method and can all be used ).

The $ _ GET variable uses "method = get" to obtain the form information. The information sent through the GET method is visible (it will be displayed in the address bar of the browser) and has a length limit (the total length of the information cannot exceed 100 characters [character]).

Case

 

After you click "Submit", the URL is displayed

"Welcome. the php file can use the "$ _ GET" variable to obtain form data (note: the name in the form field [form field] is automatically used as the ID keyword in the "$ _ GET" Array ):

Welcome <? Php echo $ _ GET ["name"];?>.
You are <? Php echo $ _ GET ["age"];?> Years old!

Why use "$ _ GET "?

Key point: when the "$ _ GET" variable is used, all variable names and variable values are displayed in the URL address bar. therefore, this method cannot be used when the information you send contains a password or other sensitive information. Because all the information will be displayed in the URL address bar, we can use it as a tag and place it in the Favorites folder. This is useful in many cases.

Note: If the variable value to be sent is too large, the http get method is not applicable. The number of sent messages cannot exceed 100 characters.

$ _ REQUEST variable

The PHP $ _ REQUEST variable contains $ _ GET, $ _ POST, and $ _ COOKIE content.

The PHP $ _ REQUEST variable can be used to obtain the form data sent through the "GET" and "POST" methods.

Case

Welcome <? Php echo $ _ REQUEST ["name"];?>.
You are <? Php echo $ _ REQUEST ["age"];?> Years old!

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

$ _ POST variable

The $ _ POST variable is an array containing the name [name] and the value [value] (these names and values are sent through the http post method and can all be used)

The $ _ POST variable uses "method = POST" to obtain the form information. The information sent through the POST method is invisible and there is no limit on the length of the information.

Case

 

After you click "Submit", the URL does not contain any form data.

"Welcome. the php file can use the "$ _ POST" variable to obtain form data (note: the name in the form field [form field] is automatically used as the ID keyword in the "$ _ POST" Array ):

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

Why use $ _ POST?

Variables sent through http post are not displayed in the URL.

There is no limit on the variable size.

However, because the variables cannot be displayed in the URL, it is impossible to store the page as a tag in favorites.

$ _ REQUEST variable

The PHP $ _ REQUEST variable contains $ _ GET, $ _ POST, and $ _ COOKIE content.

The PHP $ _ REQUEST variable can be used to obtain the form data sent through the "GET" and "POST" methods.

Case

Welcome <?php echo $_REQUEST["name"]; ?>.
You are <?php echo $_REQUEST["age"]; ?> years old!

After the above form is submitted, the PHP method for obtaining the submitted content is to share all the content with you. I hope to give you a reference, and I hope you can also provide more support for the customer's house.

PHP: why can't php obtain submitted content after submitting a form on a webpage? However, in the old version of PHP...

Related Article

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.