用户交互和表单
对于许多我们感兴趣的PHP应用来说,最基本的功能是实现与浏览这个页面的用户的交互。如果你 熟悉JavaScript,你可能会习惯于这么一种事务处理 的模式,那就是对用户的许多行动直接作出反应(例如将鼠标移动到页面上的一个连接)。而对于PHP这样的服务器端脚本程序而言,它用来实现与用户交互的活 动范围要小得多,与用户的交互仅仅发生在用户向服务器发出请求以及服务器用一个动态页面作出回应之间。
用户与PHP实现交互的关键是理解用户对一个新Web页面发出的请求中可能包含的发出信息。我们将会看到,PHP使得这个工作极为简单。
最简单的方法是使用“URL查询字符串”在页面请求中发出信息。如果你曾经看到过一个在文件名 后包含问号的URL,那就是采用的这种技术。让我们来看一个 简单的例子。建立一个标准的HTML文件(不一定使用.php扩展名,在这个文件中将不包含任何PHP代码),并在其中加入以下连接:
Hi, I'm Kevin! |
这是指向一个叫welcome.php的文件的连接,但是在连接这个文件的同时,我们还在页面 请求中传递了一个变量。这个变量是被作为“查询字符串”的一 部分传递的,它位于URL的问号后面。这个变量的名字是name,它的值是Kevin。也就是说,我们建立了一个连接,这个连接装载 welcome.php并告知这个文件中包含的PHP代码:name等于Kevin。
要清楚这么做对我们有什么好处,我们需要看看welcome.php。同样地将其作为一个新的 HTML文件来建立,但是这一次要记得使用.php 扩展名,这会告诉Web服务器在这个文件中有一些PHP代码需要解释。如果你的Web服务器还不接受.php作为PHP文件的扩展名,你可能需要将其改名 为welcome.php3(在这种情况下,你也需要相应调整上面的代码中的连接)。在这个新文件中,输入以下内容:
echo( "Welcome to our Web site, $name!" ); ?> |
现在,如果你用第一个文件中连接去装载第二个文件,你会看到这一页显示“Welcome to our Web site, Kevin!”,这个通过URL的查询字符串传递过来的变量的值被自动地赋予了一个叫$name的变量,我们在一段文本中显示了这个传递过来的变量。
如果你需要的话,你也可以通过查询字符串传递几个变量。让我们看看这个例子的稍微复杂一点的版本。将HTML文件中的连接改变为:
Hi, I'm Kevin Yank! |
这一次,我们传递了两个变量:firstname和lastname。这些变量在查询字符串中被&符号分开。你可以传递更多的变量,只要你将每一个name=value对以&符号分开。
如前所述,我们可以在我们的welcome.php文件中使用这两个变量的值:
echo( "Welcome to our Web site, $firstname $lastname!" ); ?> |
虽然看上去一切都好了,但是我们仍没有达到我们真正地与用户交互的目的,我们的用户应该能够输 入任意的信息,并将它交给PHP来处理。接着我们的个性化欢 迎页面的例子,我们想要让我们的用户任意地输入他(或她)的名字并将其显示到信息中,要让用户输入数据,我们需要用到HTML的表单。
这儿是表单的代码:
除了在这里你可以任意输入你的名字以外,这个表单所起的效果和我们上面的第二个连接(在查询字 符串中使用firstname=Kevin& lastname=Yank)完全一样。当你按提交按钮(标志为“GO”)时,浏览器会装载welcome.php并自动为你在查询字符串中添加变量和它 们的值。变量名就是在INPUT TYPE=TEXT标识中的NAME属性,变量值就是用户输入的相应的内容。
INPUT TYPE=TEXT标识中的METHOD属性是用来告诉浏览器如何在请求中发送变量名及变量值的。GET(就是我们在上面使用的)表示在查询字符串中传递 变量,但是还有另外一种选择。将变量显示在查询字符串中并不总是令我们满意的--甚至有的时候在技术上就是不可行的。如果在你的表单中包含了一个 TEXTAREA标识用来让用户输入大量的文本,这部分文本也显示在查询字符串中实在是太长了,而且会超过浏览器所能支持的URL的最大长度。另外一种方 法可以允许浏览器隐藏地传递信息。这种方法的代码和我们上面看到的表单的代码几乎一样,只是我们将表单的方法从GET变成了POST:
这个表单和我们之前的那个在功能上完全一样。唯一的不同是当用户在按“GO”按钮时所装载页面 的URL将没有一个查询字符串。一方面,这可以让你通过表单 提交大量的数据或者是敏感的数据(例如密码),而不在一个查询字符串中显示出来。另一方面,如果用户将提交表单生成的结果页添加到收藏夹,这个收藏夹是没 有用的,因为它不包含提交的数据。另外,附带说一下,象AltaVista这样的搜索引擎总是利用查询字符串来提交查询条件,其主要原因就是为了便于用户 将查询结果页添加到收藏夹中,以便在将来进行同样的搜索,因为搜索条件是包含在URL之中的。
这儿讲了利用表单来实现用户与PHP的交互的基本的原理。在以后的例子中,我们将讨论一些更高级的问题和技术。

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft