Use PHP to make a simple form _php

Source: Internet
Author: User
Keywords simple one input type form Value nam
One of the most useful features of PHP is its ability to automatically assign variable values in a form to PHP variables. This makes form processing very fast.



So, if you send out a form that contains input field values, it looks like this:



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

Therefore, you can print the values in the following ways:

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 survey that requires the fill-in person to fill in their name, email address, and answer some of the 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 one for the CGI script that handles the form), we just need to write a PHP page and use the program logic to control what the user displays.

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


Functiondisplay_form () {

Global $PHP _self;

?>




}

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



So, if you send out a form that contains input field values, it looks like this:



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

Therefore, you can print the values in the following ways:

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 survey that requires the fill-in person to fill in their name, email address, and answer some of the 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 one for the CGI script that handles the form), we just need to write a PHP page and use the program logic to control what the user displays.

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


Functiondisplay_form () {

Global $PHP _self;

?>




}

?>



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



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

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 the presence of information in a database, or the ability to query the database for information and return results in the appropriate format.

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




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

else {process_form ();}



?>



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



These are the procedures for using 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.