PHP Quick Start (2)

Source: Internet
Author: User


Form Processing

PHP provides many easy-to-use and powerful functions for users. In terms of form processing, PHP can automatically assign values to corresponding variables for the data sent by the client form, which greatly simplifies the process of form processing.

For example, the user creates the following form:

<Input type = text name = "name" VALUE = "PETER">

When PHP is used to process the above Code, PHP automatically creates a variable named $ name and assigns the variable value "PETER" to the variable.

You can directly perform various operations on the variables created in PHP, such as displaying the variable value:

Echo "Hi $ name !";

Or verify the variable value:

If ($ name = "PETER") {echo "Please check out your email .";}

Next, let's take a look at how to create and process a basic form using PHP. In the example, we will ask the form writer to answer a few short questions, including the name, email address, and questionnaire of the input form writer.

Here, we divide the page to be designed into two functional modules: Form display and form processing. In this way, users do not have to design two different pages, but only need to display or process the corresponding functional modules in the same PHP page through logical control. A. Form display

We use the first function module to display forms. The Code is as follows:

<? Php

Function display_form ()

{

Global $ PHP_SELF;

? >


<Form target = "<? Php echo $ PHP_SELF ;? > "METHOD = GET>

Name: <input type = text name = "name"> <BR>

Favorite Fruit: <input type = radio name = "fruit" VALUE = "apple"> Apple

<Input type = radio name = "fruit" VALUE = "orange"> Orange

<Input type = radio name = "fruit" VALUE = "banana"> Banana

Favorite Times to Eat Fruit:

<Input type = checkbox name = "times []" VALUE = "m"> Morning

<Input type = checkbox name = "times []" VALUE = "n"> Noon

<Input type = checkbox name = "times []" VALUE = "d"> Dinner

<Input type = checkbox name = "times []" VALUE = "l"> Latenight

<Input type = hidden name = "stage" VALUE = "results">

<Input type = submit value = "Thanks !" >

</FORM>


<? Php

}

? >

Most of the above Code is the HTML code necessary to create a form. Here, we will only give a brief introduction to the PHP knowledge involved.

First, let's take a look at the variable $ PHP_SELF at the beginning of the Code. The variable $ PHP_SELF is a convenient pointer in PHP, and its value is the URL address of the current page. In this way, we can set the TARGET value to $ PHP_SELF in the subsequent form tag to process the form from the form page. Here, we use the $ PHP_SELF variable instead of the actual address of the page, because by using the $ PHP_SELF variable, we can easily modify and move the compiled Page code, you don't have to worry about entering a new page address after each change.

In addition, when using the $ PHP_SELF variable, we adopt the following method:

Global $ PHP_SELF;

This indicates that the $ PHP_SELF variable in the Code is a global variable. When using PHP, you must note that any function variable in PHP is a local variable. This means that the scope of any function variable is limited to the function that includes the variable. Even if a variable with the same name exists outside the function, the value of the variable is different. Therefore, if we do not explicitly declare the function variable $ PHP_SELF in the Code as a global variable with a globally unique value, the user will find that the value of the variable $ PHP_SELF in the function will be null, rather than the URL address of the current page we expected in advance.

You may have noticed that we set the name of the option selected in the form to the times [] array, and set the name of the option selected to the regular variable fruit. This is because the single choice option only allows the user to select the only correct option, so the value of the fruit variable may only be a string; on the contrary, the check option allows the user to make multiple choices. If you want PHP to save all possible options, you need to use an array to save all possible values. According to the PHP syntax rules, we add square brackets after the variable name times to let PHP know that the variable is an array variable rather than a common variable.

Finally, we set an implicit variable named stage. By using this variable, we can control whether to display the form or process the form result.

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.