PHP入门速成(2)
表单处理
PHP为用户提供了众多方便易用的强大功能。在表单的处理方面,PHP能够自动地将由客户端表单发送的数据赋值给相应变量,从而极大地简化了整个表单的处理过程。
举例来说,用户建立如下表单:
当使用PHP对上述代码进行处理时,PHP自动创建一个名为$name的变量,并将变量值“PETER”赋予该变量。
用户可以直接对PHP创建的变量进行各种操作,例如显示变量值:
echo “Hi $name!”;
或对变量值进行验证:
if ($name == “PETER”) { echo “Please check out your email.”; }
下面,我们就来看一下如何通过使用PHP创建并处理一个基本的表单。在示例当中,我们将会要求表单的填写者回答几个简短的问题,包括输入表单填写者的姓名,电子邮件地址,完成调查问卷等。
这里,我们将所要设计的页面按照功能划分成表单显示和表单处理两个功能模块。这样,用户就不必设计两个不同的页面,而只需要在同一个PHP页面中通过逻辑控制显示或处理相应的功能模块。
A. 表单显示
我们通过第一个功能模块来实现表单的显示。具体代码为:
function display_form()
{
global $PHP_SELF;
? >
”METHOD=GET >
Name:
Favorite Fruit: Apple
Orange
Banana
Favorite Times to Eat Fruit:
Morning
Noon
Dinner
Latenight
}
? >
上述代码中绝大多数都是创建表单所必需的HTML代码。在此,我们仅对所涉及到的PHP知识做一下简单介绍。
首先,我们来看一下代码开始处的变量$PHP_SELF。变量$PHP_SELF是PHP中一个很方便的指针,其变量值就是当前页面的URL地址。这样,我们就可以通过在随后的form标签中将TARGET的值设定为$PHP_SELF,实现由该表单页面处理表单的目的。这里,我们使用$PHP_SELF变量而不使用页面的实际地址,是因为通过使用$PHP_SELF变量,我们可以方便对编写的页面代码进行修改和移动,而不必担心在每次改动之后重新输入新的页面地址。
另外,在使用$PHP_SELF变量时,我们采用了如下方式:
global $PHP_SELF;
这表明代码中的$PHP_SELF变量为全局变量。用户在使用PHP的过程中一定要注意PHP中任何的函数变量都是局部变量。这就意味着任何函数变量的作用域都只局限于包括该变量的函数本身。即使函数外存在同名变量,变量的值也不相同。因此,如果我们没有在代码中以显式方式声明函数变量$PHP_SELF为全局变量具有全局唯一值的话,用户就会发现函数中的变量$PHP_SELF的值将会为空,而不是我们预先所期望的当前页面的URL地址。
用户可能已经注意到我们将表单复选选项的名称设置为times[]数组,而把单选选项的名称设定为fruit常规变量。这是因为单选选项只允许用户选择唯一一个正确选项,因此fruit变量的值只可能是一个字符串;相反,复选选项则允许用户做多重性选择。如果,用户希望PHP能够保存用户可能作出的所有选择的话,就需要使用数组来保存所有的可能值。按照PHP的语法规则,我们通过在变量名times后面加上方括号,使PHP知道该变量是一个数组变量而不是普通的常规变量。
最后,我们设定了一个名为stage的隐含变量,通过使用该变量,我们可以控制是否显示表单还是处理表单结果。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

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

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
