Use PHP to make a simple form

Source: Internet
Author: User
Tags contains empty functions variables require split variable
One of the most useful features of PHP is its ability to automatically assign variable values in the form to PHP variables. This makes form processing very quick.



So if you send out a form that contains the input field values, the following:

<input type=text name= "NAME" value= "Glen Morris" >

When you work with this page in PHP, the value of the $name variable will be Glen Morris.

Therefore, you can print the value in the following way:

echo "Hi $name!";

or test its value like this:

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

Here's a good way to work with basic forms in PHP. For example, we need to do a simple questionnaire to require those who fill out their names, email addresses, and answer some questions we are interested in.

First, we'll split the page into two functions. So, instead of writing two separate pages (one for the form section and another for the CGI script that handles the form), we just need to write a PHP page and use the logic to control what's displayed to the user.

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

<?php

Functiondisplay_form () {

Global $PHP _self;

?>

<form target= "<?php echo $PHP _self;?>" method=get>

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

Favorite Cheese: <input type=radio name= "Cheese" value= "brie" >very soft French Brie

<input type=radio name= "cheese" value= "cheddar" >farmhouse Chinese cheddar

<input type=radio name= "cheese" value= "mozzarella" >italian Buffalo mozzarella

Favorite to Eat Cheese:

<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" >late Night

<input Type=hidden name= "stage" value= "Results" >

<input type=submit value= "thanks!" >

</FORM>

<?php

}

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



So if you send out a form that contains the input field values, the following:

<input type=text name= "NAME" value= "Glen Morris" >

When you work with this page in PHP, the value of the $name variable will be Glen Morris.

Therefore, you can print the value in the following way:

echo "Hi $name!";

or test its value like this:

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

Here's a good way to work with basic forms in PHP. For example, we need to do a simple questionnaire to require those who fill out their names, email addresses, and answer some questions we are interested in.

First, we'll split the page into two functions. So, instead of writing two separate pages (one for the form section and another for the CGI script that handles the form), we just need to write a PHP page and use the logic to control what's displayed to the user.

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

<?php

Functiondisplay_form () {

Global $PHP _self;

?>

<form target= "<?php echo $PHP _self;?>" method=get>

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

Favorite Cheese: <input type=radio name= "Cheese" value= "brie" >very soft French Brie

<input type=radio name= "cheese" value= "cheddar" >farmhouse Chinese cheddar

<input type=radio name= "cheese" value= "mozzarella" >italian Buffalo mozzarella

Favorite to Eat Cheese:

<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" >late Night

<input Type=hidden name= "stage" value= "Results" >

<input type=submit value= "thanks!" >

</FORM>

<?php

}

?>



First, as we do with $php_self, we get the global variables inside the form. Then check which of the cheese options is selected to create a response by choice.



Below, we use the count () function to calculate the number of periods that are selected as preference periods. In the past, it had to be done by checking $cheese every possible value, and now we can just use the larger and smaller to compare the size of the $favorite_times. If you wish to know the meaning of "&&" in "$favorite _times > 1 && $favorite _times < 4", it means "and". Therefore, the $favorite _times must be greater than one and less than four for this condition to be valid.

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

Group form
Now that we've got the two functions, then we just need to put a little piece of code together and we can connect them and build the logical relationship we need for the code. Add the following code under Display_form () and Process_form ():



<?php

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

else {process_form ();}



?>



First, we check whether the variable $stage is empty. In PHP, a variable is treated as a null value if it has not been initialized (that is, it has not been assigned) or if its value is 0 (that is, a null string or a value of 0) that is assigned a value of zero. When a user accesses a Web page for the first time, the $stage variable value is empty. So we want to display the form, otherwise we'll work on the form content.



The above is 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.