Home Backend Development PHP Tutorial PHP中的表单应用释疑_PHP

PHP中的表单应用释疑_PHP

Jun 01, 2016 pm 12:31 PM
head application us submit document

综述:表单作为用户提交信息的一个关键途径,一直是PHP编程中的一个最基本的方面,也是入门者会遇到的一个大的重点与难点。我们选择有关处理关联数、获得同名checkbox的选取值、上传文件方面比较容易令众困惑的地方进行归

如何使用表单传递关联数组?

通过表单传递的关联数组能被 each()函数读取,程序如下:

//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>
这个名为var[Address]值为"Beijing"的元素递交到test2.php后,就成了一个关联数组,var["Address"]="Beijing":
//test2.php
<?
echo $var["Address"];
?>

输出结果为:Beijing

如何处理同名checkbox?

具体代码:

test1.php:
<FORM METHOD=POST ACTION="test2.php">
苹果<INPUT TYPE="checkbox" NAME="come[]" VALUE="苹果"><BR>
鸭梨<INPUT TYPE="checkbox" NAME="come[]" VALUE="鸭梨"><BR>
香蕉<INPUT TYPE="checkbox" NAME="come[]" VALUE="香蕉"><BR>
西瓜<INPUT TYPE="checkbox" NAME="come[]" VALUE="西瓜"><BR>
<INPUT TYPE="submit" VALUE="提交">
</FORM>

test2.php:
你的选择:<BR>
<?
for ($i=0;$i<sizeof($come);$i ) echo $come[$i],"<BR>";
?>
这样从test1.php提交过来的所有名为come[]的元素就组成了一个数组,这样我们就可以很容易地处理了.

怎样才能察看提交的所有信息?

一般来说,PHP引擎将每一个表单域放到一个叫做$HTTP_POST_VARS的数组中,所以我们可以通过读取这个数组就可以察看提交的所有信息:

<?
echo "POST 所送出的值为:<BR>";
while ( list( $key, $val ) = each( $HTTP_POST_VARS ) ) {
 echo "$key => $val<BR>";
}
?>

如何同时上传多个文件?

我们来看一个例子。

下面是上传文件的提交页面,利用该页面你不仅可以生成 1000 个上传文件框(也可以是任意多个 0~n ),而且可分别指出它们的保存路径。

提交页面的文件输入框为命名为: file0,file1,...file100,...fileN
提交页面的文件路径框为命名为: path0,path1,...path100,...pathN
由于页面的生成非常简单,所以就不在此多解释了,用 javascript 定义了两个函数,check() 用于提交页面,create()用于生成文件上传框。
phpfileup.htm
--------------------------------------------------------
【文件php9.txt】
--------------------------------------------------------
文件提交页面既已生成,下面任务就很明确了:将提交的文件内容保存到服务器上。

我们先定义一个文件保存函数 fup() 它有两个参数:
$filename: 文件内容
$fname: 文件名(包含路径)
剩下的就是写一个循环将文件依次写入服务器。

PHP 对于上传文件的处理是这样的:如果提交的文件框名为 file0, 那么提交给 PHP 的文件内容保存在变量 $file0 中,而文件名则保存在 $file0_name 中。这样在这个循环中我们要做的就是将提交页面提交的内容分解出来,实现过程请看下面的代码。

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 "<BR>";

fup($$ffnn,$$ffpath.$$ffnnname); //"../www/test/tmp/"
}
?>
如何对页面进行抓取和分析?

  首先,必须决定我们将抓取的URL地址。可以通过在脚本中设定或通过$QUERY_STRING传递。为了简单起见,让我们将变量直接设在脚本中。

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

  第二步,我们抓取指定文件,并且通过file()函数将它存在一个数组里。

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

  好了,现在数组里已经有了文件了。但是,我们想分析的文本可能不全在一行里面。为了解决这个文件,我们可以简单地将数组$lines_array转化成一个字符串。我们可以使用implode(x,y)函数来实现它。如果在后面你想用explode(将字符串变量数组),将x设成"|"或"!"或其它类似的分隔符可能会更好。但是出于我们的目的,最好将x设成空格。y是另一个必要的参数,因为它是你想用implode()处理的数组。

<?
$url = 'http://www.domain.com';
$lines_array = file($url);
$lines_string = implode('', $lines_array);
?>

  现在,抓取工作就做完了,下面该进行分析了。在这个例子中,我们想得到在<head>到</head>之间的所有东西。为了分析出字符串,我们还需要正规表达式。

<?
$url = 'http://www.domain.com';
$lines_array = file($url);
$lines_string = implode('', $lines_array);
eregi("<head>(.*)</head>", $lines_string, $head);
?>

  在上面的代码中,eregi()函数按下面的格式执行:

eregi("<head>(.*)</head>", $lines_string, $head);
  "(.*)"表示所有东西,代表在<head>和</head>间的所有东西。

$lines_string是我们正在分析的字符串,$head是分析后的结果存放的数组。

  最后,我们可以输出数据。因为仅在<head>和</head>间存在一个实例,我们可以安全的假设数组中仅存在着一个元素,而且就是我们想要的。

<?
$url = 'http://www.domain.com';
$lines_array = file($url);
$lines_string = implode('', $lines_array);
eregi("<head>(.*)</head>", $lines_string, $head);
echo $head[0];
?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

How to Undo Delete from Home Screen in iPhone How to Undo Delete from Home Screen in iPhone Apr 17, 2024 pm 07:37 PM

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

The role and practical application of arrow symbols in PHP The role and practical application of arrow symbols in PHP Mar 22, 2024 am 11:30 AM

The role and practical application of arrow symbols in PHP In PHP, the arrow symbol (->) is usually used to access the properties and methods of objects. Objects are one of the basic concepts of object-oriented programming (OOP) in PHP. In actual development, arrow symbols play an important role in operating objects. This article will introduce the role and practical application of arrow symbols, and provide specific code examples to help readers better understand. 1. The role of the arrow symbol to access the properties of an object. The arrow symbol can be used to access the properties of an object. When we instantiate a pair

From beginner to proficient: Explore various application scenarios of Linux tee command From beginner to proficient: Explore various application scenarios of Linux tee command Mar 20, 2024 am 10:00 AM

The Linuxtee command is a very useful command line tool that can write output to a file or send output to another command without affecting existing output. In this article, we will explore in depth the various application scenarios of the Linuxtee command, from entry to proficiency. 1. Basic usage First, let’s take a look at the basic usage of the tee command. The syntax of tee command is as follows: tee[OPTION]...[FILE]...This command will read data from standard input and save the data to

MySQL transaction processing: the difference between automatic submission and manual submission MySQL transaction processing: the difference between automatic submission and manual submission Mar 16, 2024 am 11:33 AM

MySQL transaction processing: the difference between automatic submission and manual submission. In the MySQL database, a transaction is a set of SQL statements. Either all executions are successful or all executions fail, ensuring the consistency and integrity of the data. In MySQL, transactions can be divided into automatic submission and manual submission. The difference lies in the timing of transaction submission and the scope of control over the transaction. The following will introduce the difference between automatic submission and manual submission in detail, and give specific code examples to illustrate. 1. Automatically submit in MySQL, if it is not displayed

Create and run Linux ".a" files Create and run Linux ".a" files Mar 20, 2024 pm 04:46 PM

Working with files in the Linux operating system requires the use of various commands and techniques that enable developers to efficiently create and execute files, code, programs, scripts, and other things. In the Linux environment, files with the extension ".a" have great importance as static libraries. These libraries play an important role in software development, allowing developers to efficiently manage and share common functionality across multiple programs. For effective software development in a Linux environment, it is crucial to understand how to create and run ".a" files. This article will introduce how to comprehensively install and configure the Linux ".a" file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux ".a" file. What is L

Explore the advantages and application scenarios of Go language Explore the advantages and application scenarios of Go language Mar 27, 2024 pm 03:48 PM

The Go language is an open source programming language developed by Google and first released in 2007. It is designed to be a simple, easy-to-learn, efficient, and highly concurrency language, and is favored by more and more developers. This article will explore the advantages of Go language, introduce some application scenarios suitable for Go language, and give specific code examples. Advantages: Strong concurrency: Go language has built-in support for lightweight threads-goroutine, which can easily implement concurrent programming. Goroutin can be started by using the go keyword

What should I do if the lib file is missing in the Linux system? What should I do if the lib file is missing in the Linux system? Mar 19, 2024 pm 03:03 PM

Title: What should I do if the lib file is missing in the Linux system? When using a Linux system, sometimes you encounter missing lib files, which may cause the program to fail to run properly. This article describes some ways to solve this problem and provides specific code examples. 1. Error prompts: When the program is running in the Linux system, if the necessary dynamic link library (lib) is missing, a prompt similar to the following will appear: errorwhileloadingsharedlibrari

See all articles