PHP form processing for instance learning

Source: Internet
Author: User
P Title: ■ preparations before learning PHP form processing for instance learning: to learn PHP, you must install PHP. so if you are learning PHP for the first time, please read the network school article: PHP4.03 install PHP4.04 in linux and PHP4.04 install PHP4.04 in win98. if you cannot find the installer, please download it below: PHP4. P> Title: ■ instance learning PHP form processing
Preparations before learning:

If you want to learn PHP, you must install PHP. if you are studying PHP for the first time, read the articles of the network school:

PHP4.03 installation in linux

PHP4.04 installation under win98

PHP4.04 installation in win2000

If you cannot find the installer, please download it below:

PHP4.04Beta WIN32 installer


PHP4.03 source code


PHP3.0.16 WIN32 installer


PHP3.0.16 source code

OK! There should be nothing to stop us from learning now, Let's go!

Before learning, let's explain the basic knowledge about the form. if you are familiar with HTML, skip this section and learn it directly.

Therefore, the website form is a tool used to collect data from webpages. for example, if you want to conduct a mass investigation on the Internet, you must use it. Next I will briefly introduce the basic knowledge of the form. for details about the form, please refer to the HTML Manual.

The use of forms is actually very simple. let's take a look at the example below:

  


<FORM action = "<? Php echo $ PHP_SELF;?> "METHOD = post>

NAME: <input type = TEXT name = "NAME">


Single choice:

<Input type = radio name = "first" VALUE = "I am stupid"> I am stupid

<Input type = radio name = "first" VALUE = "I am very stupid"> I am very stupid

<Input type = radio name = "first" VALUE = "I am a dumb"> I am a dumb.


Multiple options:

<Input type = checkbox name = "second []" VALUE = "I like playing basketball"> I like playing basketball.

<Input type = checkbox name = "second []" VALUE = "I like swimming"> I like swimming.

<Input type = checkbox name = "second []" VALUE = "I like dancing"> I like dancing.

<Input type = checkbox name = "second []" VALUE = "I like hiking"> I like hiking.

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

<Input type = submit value = "thank you">

</FORM>

Name:
Single choice: I'm stupid. I'm stupid.
Multiple options: I like playing basketball, I like swimming, I like dancing, and I like hiking.


How is it? Do you understand? The above part is the form's html source code, and the lower part is the form's representation in the browser. Flag <FORM action = "<? Php echo $ PHP_SELF;?> "METHOD = post> indicates that a Form starts and ends with the flag </Form>. The program that processes this Form is indicated by the action attribute in the Form Flag. <? Echo $ PHP_SELF;?>, $ PHP_SELF is a global variable in PHP. it is used to save the file name of the page currently running the PHP program. <? Echo $ PHP_SELF;?> The current PHP program is used to process the form. So what does the following METHOD = post mean? In fact, there are two ways to send a form from the browser to the server: GET and POST. GET to package the data and place it in the environment variable QUERY_STRING as part of the whole URL and pass it to the server. POST does many similar GET Operations. The difference is that it transmits data to the script separately. your script obtains the data through standard input. the QUERY_STRING environment variable will not be set. therefore, POST has better security, especially if your form contains a lot of data. when you use GET, the server assigns the variable QUERY_STRING to all form data, but the variable has limited storage capacity. in other words, if you have a lot of data but you use GET, you will lose a lot of data. If you use POST, you can use as much data as possible, because the data is never allocated to a variable. In addition, using post to transmit data does not expose the data you transmit to the address bar of a browser like get, for example, form. php? Name = genius & pwd = 123456, do you understand? So it is reassuring to use post.

(Editor's Note: due to database compatibility issues, replace the "$" character and the quotation mark in the source code with the corresponding symbol of the halfwidth character in practice. Sorry for any inconvenience .)

Let's continue with our study. in the process of creating a Form, you generally only use two logos, one being the one mentioned above; Form> flag, and the other being the; Input> flag, don't underestimate it. it is used to create various widgets in the form. The Type attribute of the Input flag defines the Type of the Input method. Valid values include TEXT, PASSWORD, RADIO, CHECKBOX, SUBMIT, RESET, IMAGE, FILE, HIDDEN, and BUTTON. The following is a brief description of these input types:

TEXT generates a simple single line TEXT input field. to obtain multiple lines of TEXT, use the; TEXTAREA> flag
PASSWORD and TEXT generate simple single-line TEXT input fields, but the input content is not displayed on the screen
RADIO generates small single-choice buttons that can be opened or closed, and multiple options can be CHECKBOX or; SELECT> flag
SUBMIT generates a button to SUBMIT all name value pairs to the form handler.
RESET is used to RESET all input methods to null or default values.
The IMAGE changes the submit button to a graph. this value is the same as the submit button, but the x and y coordinates of the cursor are also sent to the form processing program when the IMAGE is selected.
BUTTON generates a BUTTON that does not interact with the script.


Now we know how to generate input widgets. how can we transmit the input data in these widgets to our programs for processing? In the INPUT mark, d has a NAME attribute specifically used to NAME the INPUT value, for example, NAME = "first ". After definition, if you want a program to access this data, you can use the global statement. If you want to define the data default Value, you can use the VALUE attribute to define it. for example, the above program Value = "I am stupid" is to define a default VALUE. let's look at an instance of a single choice:

; Input type = radio name = "first" VALUE = "I am stupid"> I am stupid

; Input type = radio name = "first" VALUE = "I am very stupid"> I am very stupid

; Input type = radio name = "first" VALUE = "I am a dumb"> I am a dumb.

Can we see from the above that the basic usage of NAME and VALUE has been made? What? I still don't understand ............ (Hiding and fainting ......)

OK. Let's simply use the form here. let's start with the topic ----- PHP processing form.

It is really easy to use PHP to process form data. for example, it is just like using chopsticks when you eat. it is very natural to use, and there is nothing to pay special attention. The old saying goes, let's take a look at the following code before learning.

The above is a PHP example of a very similar questionnaire, because the program is relatively simple, so there is no annotation. Before carefully reading the program, you can copy the code and run it on your machine to see the actual effect. This gives you an intuitive impression.

(Editor's Note: due to database compatibility issues, replace the "$" character and the quotation mark in the source code with the corresponding symbol of the halfwidth character in practice. Sorry for any inconvenience .)

Questionnaire form;/title> </p>

Function display_form (){

Global $ PHP_SELF;

?>

}

?>

// Start the program

Function process_form ()

{

Global $ name;

Global $ first;

Global $ second;


If ($ first = 'I am dump') {$ first_message =' you are not stupid. ';}

Elseif ($ first = 'I am dump') {$ first_message =' You are very smart. ';}

Else {$ first_message = 'You are like a smart person. ';}

$ Favorite_second = count ($ second );

If ($ favorite_second; = 1)

{$ Second_message = 'but you will soon die in the zoo. sorry! ';}

Elseif ($ favorite_second> 1 & $ favorite_second; 4)

{$ Second_message = 'you are a sports-loving orangutan. ';}

Else {$ second_message = 'You have too many sports, and you have already done too much for the orangutan. prepare a coffin ,:(';}

Echo "this is a test for orangutan:; br> ";

Echo "Hello! Your name is $ name.; br> ";

Echo "your test result is ..... $ First_message $ second_message ";

}

?>

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

Else {process_form ();}

?>

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.