Home php教程 php手册 PHP中的表单应用释疑

PHP中的表单应用释疑

Jun 21, 2016 am 09:04 AM
file head lines name php

综述:表单作为用户提交信息的一个关键途径,一直是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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles