Summary: as a key way for users to submit information, forms have always been the most basic aspect of PHP programming, and are also a major point and difficulty for beginners. How to use the form to pass the associated array when dealing with the number of associations, obtaining the checkbox value of the same name, and uploading files is confusing? Connect
Summary:As a key way for users to submit information, forms have always been the most basic aspect of PHP programming, and are also a major key and difficulty for beginners. We choose a location that is confusing when dealing with the number of associations, getting the checkbox of the same name, and uploading files.
How do I use forms to pass an associated array?
The associated array passed through the form can be read by the each () function. 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 = "20">
<Input type = submit value = submit>
After the element named var [Address] with the value "Beijing" is submitted to test2.php, it becomes an associated array. var ["Address"] = "Beijing ":
// Test2.php
<?
Echo $ var ["Address"];
?>
Output result: Beijing
How to handle checkbox with the same name?
Code:
Test1.php:
<Form method = post action = "test2.php">
Apple <input type = "checkbox" NAME = "come []" VALUE = "apple"> <BR>
Yali <input type = "checkbox" NAME = "come []" VALUE = "Yali"> <BR>
Banana <input type = "checkbox" NAME = "come []" VALUE = "banana"> <BR>
Watermelon <input type = "checkbox" NAME = "come []" VALUE = "watermelon"> <BR>
<Input type = "submit" VALUE = "submit">
</FORM>
Test2.php:
Your choice: <BR>
<?
For ($ I = 0; $ I <sizeof ($ come); $ I) echo $ come [$ I], "<BR> ";
?> In this way, all the elements submitted from test1.php named come [] form an array, which can be easily processed.
How can I view all submitted information?
In general, the PHP engine places each form field in an array called $ HTTP_POST_VARS, so we can read this array to view all submitted information:
<?
Echo "the value sent by POST is: <BR> ";
While (list ($ key, $ val) = each ($ HTTP_POST_VARS )){
Echo "$ key => $ val <BR> ";
}
?>
How to upload multiple files at the same time?
Let's look at an example.
The following is the submission page for uploading files. using this page, you can not only generate 1000 Upload file boxes (or any number of 0 ~ N), and you can specify their storage paths.
The file input box on the submit page is named: file0, file1,... file100,... fileN
The file path box on the submit page is named path0, path1,... path100,... pathN.
Because the page generation is very simple, I will not explain it here. two functions are defined using javascript. check () is used to submit the page, and create () is used to generate a file upload box.
Phpfileup.htm
--------------------------------------------------------
Example file php9.txt]
--------------------------------------------------------
The following task is clear when the file submission page is generated: save the submitted file content to the server.
We first define a file storage function fup () which has two parameters:
$ Filename: file content
$ Fname: file name (including path)
The rest is to write a loop to write files to the server in sequence.
PHP processes uploaded files as follows: if the submitted file box is named file0, the file content submitted to PHP is saved in the variable $ file0, while the file name is saved in $ file0_name. In this way, what we need to do in this loop is to break down the content submitted on the submitted page. for the implementation process, see the following code.
For ($ I = 0; $ I <$ cnt; $ I ){
$ Ffnn = "file". $ I;
$ Ffnnname = $ ffnn. "_ name ";
$ Ffpath = "path". $ I;
// Print $ ffnn;
Print $ ffnnname;
Print "<BR> ";
Fup ($ ffnn, $ ffpath. $ ffnnname); // "../www/test/tmp /"
}
?>
How to capture and analyze pages?
First, we must determine the URL we will capture. It can be set in the script or passed through $ QUERY_STRING. For simplicity, let's set the variables directly in the script.
<?
$ Url = 'http: // www.domain.com ';
?>
Step 2: capture the specified file and store it in an array using the file () function.
<?
$ Url = 'http: // www.domain.com ';
$ Lines_array = file ($ url );
?>
Now the file is in the array. However, the text we want to analyze may not be all in one line. To solve this problem, we can simply convert the array $ lines_array into a string. We can use the implode (x, y) function to implement it. If you want to use explode (array of string variables), set x to "|" or "! "Or other similar separators may be better. But for our purpose, it is best to set x to a space. Y is another necessary parameter because it is an array that you want to process with implode.
<?
$ Url = 'http: // www.domain.com ';
$ Lines_array = file ($ url );
$ Lines_string = implode ('', $ lines_array );
?>
Now, the crawling is finished, and the analysis is as follows. In this example, we want to get everything from
<?
$ Url = 'http: // www.domain.com ';
$ Lines_array = file ($ url );
$ Lines_string = implode ('', $ lines_array );
Eregi ("?>
In the above code, the eregi () function is executed in the following format:
Eregi (""(. *)" Indicates everything, representing everything between
$ Lines_string is the string we are analyzing, and $ head is the array of the analysis result.
Finally, we can output data. Because there is only one instance between
<?
$ Url = 'http: // www.domain.com ';
$ Lines_array = file ($ url );
$ Lines_string = implode ('', $ lines_array );
Eregi ("Echo $ head [0];
?>
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.