The form application in PHP is a simple explanation

Source: Internet
Author: User
Summary:As a key way for users to submit information, form is always one of the most basic aspects of PHP programming, and it is also 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, to upload the file is more easily confusing place 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 procedure is as follows:

test1.php


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 result is: Beijing

£ How do I handle a checkbox with the same name?

Specific code:

test1.php:
Apple

Pear

Banana

Watermelon



test2.php:
Your choice:

?
For ($i =0 $i ";
? >
So all the elements named "come[" from test1.php are composed of an array so that we can handle it easily.

£ 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:

?
The value sent by echo "POST" is:
;
while (List ($key, $val) = each ($HTTP _post_vars)) {
echo "$key => $val
";
£}
? >

£ How do I upload multiple files at the same time?

Let's take a look at an example.

The following 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, so there is no more explanation, with JavaScript defined two functions, check () for the submission page, create () to generate 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 (include path)
The rest is to write a loop writes the file to the server sequentially.

php for uploading files: If the submitted file frame is named File0, the contents of the file submitted to PHP are saved in the variable $file 0, and the file name is saved in $file 0_name. So what we're going to do in this cycle is to break down the content submitted by the submission page and see the code below for the implementation process.

fileup.php
< function fup ($Local _file_name, $Remote _file_name) {
if ($Local _file_name!= "None") {
copy ($Local _file_name, $Remote _file_name);
unlink ($Local _file_name);
}
£}

for ($i =0; $i $cnt; $i + +) {
 $ffnn = "file". $i;
 $ffnnname = $ffnn. " _name ";
 $ffpath = "path". $i;

//print $ $FFNN;
print $ $ffnnname;
print "
";

fup ($ $FFNN, $ $ffpath. $ $ffnnname); //".. /www/test/tmp/"
£}
?>
 how to crawl and analyze a page?

First, you must decide which URL address we will crawl. Can be passed in script or through $query_string. For simplicity's sake, let's set the variables directly in the script.

?
$url = ' http://www.domain.com ';
? >

In the second step, we crawl the specified file and we use the file () function to put it in an array.

?
$url = ' http://www.domain.com ';
$lines _array = file ($url);
? >

OK, now we have the file in the array. However, the text we want to analyze may not be all in one line. To solve this file, 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 (the array of string variables) in the back, set X to "|" or "!" or other similar delimiters might be better. But for our purposes, it is best to set the X as a space. Y is another necessary parameter because it is an array you want to process with implode ().

?
$url = ' http://www.domain.com ';
$lines _array = file ($url);
$lines _string = Implode (", $lines _array);
? >

Now, the crawl work is done, the following analysis. In this example, we want everything between to . In order to parse out the strings, we also need regular expressions.

?
$url = ' http://www.domain.com ';
$lines _array = file ($url);
$lines _string = Implode (", $lines _array);
Eregi (" (. *) ", $lines _string, $head);
? >

In the above code, the Eregi () function executes in the following format:

Eregi (" (. *) ", $lines _string, $head);
"(. *)" means everything, representing everything between and .

$lines _string is the string we are analyzing, $head is an array of the results of the analysis.

Finally, we can output the data. Because there is only one instance between and , we can safely assume that there is only one element in the array, and that's what we want.

?
$url = ' http://www.domain.com ';
$lines _array = file ($url);
$lines _string = Implode (", $lines _array);
Eregi (" (. *) ", $lines _string, $head);
echo $head [0];
? >



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.