Home > Backend Development > PHP Tutorial > How does php handle forms?

How does php handle forms?

怪我咯
Release: 2023-03-10 19:20:02
Original
1612 people have browsed it

One very useful feature of PHP is the way it handles PHP forms. A very important principle to understand is that any element of the form automatically takes effect in the PHP script. See "External Variables in PHP" in this manual for details and examples of using forms in PHP. Here is an example of an HTML form:

<form action="action.php" method="post">
 <p>姓名: <input type="text" name="name" /></p>
 <p>年龄: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>
Copy after login

There is nothing special about this form; no special identifiers are used. When the user fills out the form and clicks the submit button, the page action.php will be called. In this file, you can add the following:

Example #2 Print data from the form

你好,<?php  echo  htmlspecialchars ( $_POST [ &#39;name&#39; ]);  ?>。
你 <?php  echo (int) $_POST [ &#39;age&#39; ];  ?> 岁了。
Copy after login

The output of this script may be:

你好,Joe。你 22 岁了。
Copy after login

In addition to htmlspecialchars() and (int) part, it is obvious what this program does. htmlspecialchars() enables the special characters in HTML to be correctly encoded, so that users will not inject HTML tags or Javascript codes into the page. For example, for the age field, we clearly know it is a numeric value, so we convert it to an integer to automatically eliminate any unnecessary characters. You can also use PHP's filter extension to automatically complete this work. PHP will automatically set the $_POST['name'] and $_POST['age'] variables. Before this we used Super global variables $_SERVER, now we have introduced the super global variable $_POST, which contains all POST data. Please pay attention to the method of submitting data in our form. If the GET method is used, the information in the form will be stored in the superglobal variable $_GET. If you don't care about the source of the requested data, you can also use the superglobal variable $_REQUEST, which contains all GET, POST, COOKIE and FILE data.

It is also possible to handle XForms input in PHP, although users may prefer to use the long-standing and well-supported HTML forms. XForms is not yet suitable for beginners, but it may be of interest to users. The manual has a section in the "Features" chapter that gives a brief introduction to how to handle data received from XForum.

The above is the detailed content of How does php handle forms?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template