The form application in PHP is a simple explanation

Source: Internet
Author: User
Tags pear php programming

Summary: As a key way for users to submit information, form is always one of the most basic aspects of PHP programming, as well as a big focus and difficulty that beginners will encounter. We choose to deal with the number of associations, to get the selected value of the checkbox with the same name, upload files, more easily confusing places to return

How do I use a form to pass an associative array?

The associative array passed through the form can be read by each () function, and the program is as follows:

test1.php
<form action= "test2.php" method=post>
<input Type=hidden name= "var[address]" value= "Beijing" >
<input type=hidden name= "var[' Age '" "value=" >
<input Type=submit value=submit>
After the element named "Beijing" is submitted to the test2.php, it becomes an associative array, var["address"]= "Beijing": var[address.
test2.php
?
echo $var ["Address"];
?>

The output is: Beijing

How do I handle a checkbox with the same name?

Specific code:

test1.php:
<form method=post action= "test2.php" >
Apple <input type= "checkbox" Name= "come[" value= "Apple" ><BR>
Pear <input type= "checkbox" Name= "come[]" value= "pear" ><BR>
Banana <input type= "checkbox" Name= "come[" value= "Banana" ><BR>
Watermelon <input type= "checkbox" Name= "come[" value= "Watermelon" ><BR>
<input type= "Submit" value= "submitted" >
</FORM>

test2.php:
Your choice:<br>.
?
For ($i =0 $i <sizeof ($come); $i) echo $come [$i], "<BR>";
?>
So all the elements named "come[" from test1.php are composed of an array so that we can easily handle it.

How can I view all the information submitted?

In general, the PHP engine places each form field in an array called $http_post_vars, so we can read the array to see all the information submitted:

?
echo "POST sends a value of:<br>";
while (the list ($key, $val) = each ($HTTP _post_vars)) {
echo "$key => $val <BR>";
}
?>

How do I upload multiple files at the same time?

Let's take a look at an example.

Here is the Upload file Submission page, use this page you can not only generate 1000 upload file box (can also be any number of 0~n), and can point out their save path respectively.

The file input box for the Submit page is named: File0,file1,... file100,... filen
The File path box for the Submit page is named: path0,path1,... path100,... pathn
Because the generation of the page is very simple, it is not explained here, two functions are defined with JavaScript, check () is used to submit the page, create () is used to generate the File upload box.
Phpfileup.htm
--------------------------------------------------------
"File Php9.txt"
--------------------------------------------------------
The file submission page has been generated and the following task is clear: Save the submitted file contents to the server.

We first define a file Save function fup () it has two parameters:
$filename: File contents
$fname: File name (including path)
All that's left is to write a loop to write the file to the server sequentially.

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.