Use php to make a simple form

Source: Internet
Author: User
One of the most useful features of PHP is that it can automatically assign variable values in the form to PHP variables. This makes form processing very quick. Therefore, if you send out a form containing the input field value: INPUTTYPETEXTNAMEnameVALUEGlenMorris when you use PHP to process this page, the value of the $ name variable will be GlenMorris. Therefore, one of the most useful features you can use PHP is that it can automatically assign the variable value in the form to the PHP variable. This makes form processing very quick.



Therefore, if you send a form containing the input field value, the following is returned:



When you use PHP to process this page, the value of the $ name variable will be Glen Morris.

Therefore, you can print the value as follows:

Echo "Hi $ name! ";

Or test its value like this:

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

There is a good way to process basic forms with PHP. For example, we need to make a simple survey and ask the writers to fill in their names, email addresses, and answer some questions we are interested in.

First, we split the page into two functions. Therefore, we only need to write a PHP page and use the program logic to control the user's display content, instead of writing two separate webpages (one for the form part, another CGI script for processing forms ).

Display form
The first function is used to display the form:


Functiondisplay_form (){

Global $ PHP_SELF;

?>




}

?>
One of the most useful features of PHP is that it can automatically assign variable values in the form to PHP variables. This makes form processing very quick.



Therefore, if you send a form containing the input field value, the following is returned:



When you use PHP to process this page, the value of the $ name variable will be Glen Morris.

Therefore, you can print the value as follows:

Echo "Hi $ name! ";

Or test its value like this:

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

There is a good way to process basic forms with PHP. For example, we need to make a simple survey and ask the writers to fill in their names, email addresses, and answer some questions we are interested in.

First, we split the page into two functions. Therefore, we only need to write a PHP page and use the program logic to control the user's display content, instead of writing two separate webpages (one for the form part, another CGI script for processing forms ).

Display form
The first function is used to display the form:


Functiondisplay_form (){

Global $ PHP_SELF;

?>




}

?>



First, we get the global variables in the form just like we do $ PHP_SELF. Then, check which one of the cheese options is selected to create a response based on the selected one.



Next, we use the count () function to calculate the number of time periods selected as the preferred time period. In the past, we had to check every possible value of $ cheese. now we only need to compare the size of $ favorite_times with the value greater than or less than $ cheese. If you want to know the meaning of "&" in "$ favorite_times> 1 & $ favorite_times <4", it indicates "and ". Therefore, the $ favorite_times condition must be greater than one and less than four.

At the end of the code, we output the user name and the information we provide to him. Of course, you can also implement a variety of interesting changes, such as storing information in a database or querying information in a database and returning results in an appropriate format.

Combination form
Now we have obtained the two functions, and then we only need to put a small piece of code to connect them together and establish the logical relationship required by the code. Add the following code under display_form () and process_form:




If (empty ($ stage) {display_form ();}

Else {process_form ();}



?>



First, check whether the variable $ stage is empty. In PHP, a variable is considered null if it has not been initialized (that is, it has not been assigned a value) or its value is zero (that is, it is assigned a zero value, that is, a blank string or zero value. When a user visits a webpage for the first time, the $ stage variable value is blank. Therefore, we want to display the form; otherwise, we will process the form content.



The above describes how to use PHP to process forms.

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.