When it comes to Web development, we have to mention HTML, which has been the de facto standard for Web user interface design for several years. Although the current use of page scripts such as WAP/XML makes it difficult to maintain the dominance of HTML, if you want to write front-end PHP web applications, developers still need to understand the HTML language, especially the form part of HTML. In this chapter, we will learn the following: ? Use Dreamweaver to design HTML forms ? Use PHP to send and receive form data ? PHP form multi-page value transfer and processing ? Use PHP to verify user input ? Prevent in PHP Some lightweight attacks ? Two session management methods of PHP: COOKIE and SESSION ? Planning our web applications 5.1 Forms and HTML HTML is a simple markup language that provides users with great Flexibility, which makes it easy to learn and write. It is also because of this that too many web designers almost abuse the design and coding of HTML, causing a page to run in several different browsers such as IE, Firefox, and Mozila. shown to be very different. Today’s web design has enabled new standards, aiming to make the HTML of web pages only contain content and information, and store information in standard HTML and CSS (cascading style sheets), which is the now popular DIV+CSS design standard. Some people suggest using XML to replace HTML language. Although XML has such powerful functions, the entry barrier is high and daunting, and there are currently too many HTML-based websites. Therefore, the current standard is the compatible specification of HTML and XML, called XHTML, which is used to Transition from HTML to XML. The code in this book is based on XHTML compatibility, and it is recommended that you also apply XHTML to Web projects. Creating and processing forms is an important competency indicator for PHP developers. Next we start to introduce how to design the form. Forms are the most commonly used components in web applications, consisting of submit buttons and other related elements. Forms are used in various fields to implement functions such as registering users, filling in bank accounts, and logging in. The form uses as the start tag and ends with , otherwise it will have no effect. Several forms are allowed in an HTML page, and the name and Form ID of the form are used as the distinction between them when writing. The following is the simplest form, the code is as follows:
This form will only display a button on the browser "Submit query content" "The words don't have much meaning. If you want to submit data and form a complete form, you need to add two important attribute tags to the tag: action and method, as shown in the following form:
where the action tag refers to the file location that receives the processing result. When the action value is empty, it is submitted to the current file itself. If the action value is other file or URL, then submit it to the file or URL address for processing. The method tag describes the method used when submitting data. It has two values: GET and POST. If the method attribute is not set or the attribute is empty, the browser defaults to the POST method. Here’s how to handle POST forms. Example 5-1: getPasswd.php – Accept values submitted by POST form $action = $_SERVER['PHP_SELF']; if ($_SERVER['REQUEST_METHOD'] == 'POST') { echo 'Use POST method to pass form values'; echo "$_POST[email]"; } ?>
If you want to send a form or data to the server in the browser, you can use the GET or POST method. The GET method uses the browser address bar to pass the value when accessing the URL. We can see this kind of URL string on many websites. What is shown in Figure 5-1 is to use the GET method to pass parameters.
Error-prone, and the length of the string passed by GET cannot exceed 250 characters If the characters are too long, the browser will automatically truncate them, resulting in data loss. In addition, the GET method does not support any characters other than ASCII characters. For example, when it contains Chinese characters or other non-ASCII characters, additional encoding operations are required, although sometimes the browser can automatically complete it (you can use the url_encode and url_decode functions, use See section 2.9.2 for details on the method). When the POST method sends variable data, it is opaque to the user. According to the HTTP protocol, the data is attached to the header information of the header and the user cannot modify it at will. This is much safer for web applications. And using POST, you can send large volumes of data to the web server. Because POST is sent together with the HTTP header information, when the POST form submission is triggered, if the user clicks the "Back" button while browsing the page, the browser will not automatically resend the POST data. If the user clicks the "Refresh" button at this time, there will be a prompt "The data has expired, do you want to resubmit the form?" This is not as convenient as GET. When using GET to pass values, even if the user uses the "Back" or "Refresh" button, the browser's URL address still exists. Therefore, we need to flexibly choose GET and POST to submit form data according to the actual application during development. It is worth mentioning that if the form closing tag is missing in HTML, then the entire form will not trigger any submission action. During actual development, some careless people will find that nothing happens when clicking the button. In fact, just check the form code carefully. Sometimes even if one HTML character is missing, the browser will not do the work for us. 5.4 Form Elements There are more than a dozen tag elements used in forms. The commonly used and important tags in PHP development are shown in Table 5-1. Table 5-1 Form element description input type="checkbox" checkbox, allowing users to select multiple options input type="file" file browsing box, which can be used to open a modal window when a file is uploaded To select a file input type="hidden" hidden label, used to implicitly submit variable values in the form input type="password" Password text box, when the user enters characters in the text box, it will be replaced and displayed as * No. input type="radio" single option, used to set a set of options, the user can only select one input type="reset" to clear and reset the content of the form, used to clear the content of all text boxes in the form, and Restore the selection menu items to their initial values input type="submit" form submission button input type="text" single-line text box select drop-down list box, which can be single-selected or multi-selected. The default is single selection. If you want to add multiple selection function, add
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