P>标题: ■ 实例学习 PHP 之表单处理篇
学习前的准备:
要想学习PHP,当然少不了要安装PHP啦,所以如果你是初次学习,请先阅读网络学院的文章:
PHP4.03在linux下的安装
PHP4.04在win98下的安装
PHP4.04在英文win2000下的安装
如果你找不到安装程序请到下面下载:
PHP4.04Beta WIN32 安装程序
PHP4.03源程序
PHP3.0.16 WIN32 安装程序
PHP3.0.16源程序
OK!现在应该已经没有什么可以阻止我们学习了,Let's go!
在学习开始以前,我们先交待一下关于表单的基础知识,如果你对HTML非常熟悉,那么可以跳过该部分,直接学习。
故名思议表单是利用网页收集数据的工具,比如你想在网上搞个群众调查啊什么的,肯定是少不了要用他的。下面我简单介绍一下表单的基础知识,关于他的详细内容请自行查阅HTML手册。
表单的使用其实非常简单,大家先看一下下面的例子:
"METHOD=post>
名字:
单项选择:
我很笨
我非常笨
我简直就是个傻冒
多项选择:
我喜欢打蓝球
我喜欢游泳
我喜欢跳舞
我喜欢爬山
名字:
单项选择: 我很笨 我非常笨 我简直就是个傻冒
多项选择: 我喜欢打蓝球 我喜欢游泳 我喜欢跳舞 我喜欢爬山
怎么样?看明白了吗?上部分是表单的html源代码,下部分则是这个表单在浏览器的表现形式。 标志"METHOD=post> 表示开始一个表单,到标志时表单结束。处理这个表单的程序,用Form标志里的action属性指出。在这里为, 其中$PHP_SELF是PHP中的一个global 变量,用于保存目前执行 PHP 程式页面的档名,的意思就是用当前的PHP程序来处理这个表单。那么下面的METHOD=post表示什么意思呢?其实表单从浏览器发给服务器有两种方法. GET 和 POST. GET方法将数据打包放置在环境变量QUERY_STRING中作为URL整体的一部分传递给服务器。 POST做很多类似GET同样的事情, 不同的地方就是它是分离地传递数据给脚本. 你的脚本通过标准输入获取这些数据. QUERY_STRING环境变量将不再设置.因此POST有更好的安全性,尤其如果你的表单中有很多数据的话. 当你用GET, 这个服务器就分配变量QUERY_STRING给所有的表单数据, 但是这个变量可存储量是有限的. 换句话说,如果你有很多数据但是你又用GET,你会丢失很多数据。 如果你用POST, 你可以尽可能多地使用数据, 因为这些数据从来也不分配到一个变量里。此外用post传递数据还有一个好处,它不会象get那样把你传送的数据暴露在浏览器的地址栏中,比如象下面这种:form.php?name=genius&pwd=123456 ,明白了吧?所以还是用post让人安心一点啦。
(编者注:由于数据库的兼容问题,在上文的源代码中的“$”符和上引号请在实际应用中改为半角字符的相应符号。若有不便之处,敬请原谅。)
下面继续我们的学习吧,在建立表单的过程中,你一般只会用到两个标志,一个是前面说的; Form>标志,还有就是 ; Input>标志,不要小看它哟,表单里的各种小控件都是由它来建立的。借助于Input标志的Type属性可以定义输入方法类型,有效值为TEXT,PASSWORD,RADIO,CHECKBOX,SUBMIT,RESET,IMAGE,FILE,HIDDEN和BUTTON。 下面是对这几个输入类型的简要说明:
TEXT 产生简单的单行文本输入字段,为了取得多行文本,用; TEXTAREA>标志
PASSWORD 和TEXT一样产生简单的单行文本输入字段,但不会在屏幕上显示输入内容
RADIO 产生可开闭的小单选按钮,多项选择可用CHECKBOX型或者; SELECT>标志
SUBMIT 产生用于将所有名称数值对提交给表单处理程序的按钮
RESET 用于将所有输入方法复位为空值或缺省值
IMAGE 将提交按钮变成图形,这个数值与提交按钮一致,只是选择图形时鼠标位置x、y坐标也发送给表单处理程序
BUTTON 产生不与脚本进行特定交互功能的按钮
现在我们已经知道如何生成输入小控件了,那么在这些控件里输入的数据怎么传递给我们的程序来处理呢?在d 在INPUT标志中有一个NAME属性专门用于给输入的数值取名,例如: NAME="first"。在定义以后,如果想程序访问这个数据,用global申明一下就可以使用喽。如果想定义数据默认值的话,可以用Value属性定义,例如上面程序的 VALUE="我很笨" 就是定义了一个默认值,下面我们来看一个单项选择的实例:
; INPUT TYPE=RADIO NAME="first" VALUE="我很笨">我很笨
; INPUT TYPE=RADIO NAME="first" VALUE="我非常笨">我非常笨
; INPUT TYPE=RADIO NAME="first" VALUE="我简直就是个傻冒"> 我简直就是个傻冒
从上面大家可以看出NAME,VALUE的基本用法了吗?什么?还不明白…………(地藏晕倒在地……)
OK,我们的表单使用就简单的讲到这里吧,下面开始我们进入正题 ----- PHP处理表单
用PHP来处理表单数据实在是一件非常简单的事情,打个比方就好象你吃饭时用筷子一样,使用非常的自然,没有什么需要特别注意的地方。老样子,大家开始学习以前先看看下面的代码吧。
上面是一个非常类似调查表的PHP例子,由于程序比较简单,所以没有做什么注解。大家在仔细看程序之前可以把这段代码拷下来在自己机器运行一下先看看实际效果,这样有一个直观的印象。
(编者注:由于数据库的兼容问题,在上文的源代码中的“$”符和上引号请在实际应用中改为半角字符的相应符号。若有不便之处,敬请原谅。)
function display_form() {
global $PHP_SELF;
?>

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment