Table of Contents
Web交互
1.Web表单交互
2.URL参数交互
3.数组方式提交数据
4.HTML特殊字符处理
Home Backend Development PHP Tutorial Analysis of interactive operation examples between PHP and Web pages

Analysis of interactive operation examples between PHP and Web pages

Jul 16, 2020 pm 05:45 PM
php

Analysis of interactive operation examples between PHP and Web pages

本文实例讲述了PHP与Web页面交互操作。分享给大家供大家参考,具体如下:

Web交互

1.Web表单交互

  • 当表单的method属性提交方式为POST时,浏览器发送POST请求
  • 当表单的method属性提交方式为GET时,浏览器发送GET请求
     当PHP收到来自浏览器提交的数据后,会自动保存到超全局变量中。

超全局变量是PHP预定义好的变量,可以再PHP脚本的任何位置使用

  • 常见的超全局变量数组变量有$ _POST、$_GET等
  • 通过POST方式提交的数据会保存到$_POST中
  • 通过GET方式提交的数据会保存到$_GET中

相关学习推荐:PHP编程从入门到精通

2.URL参数交互

当表单以GET方式提交时,会将用户填写的内容放在URL参数中进行提交。
表单的method属性删除(或将其值改为get),然后提交表单,会得到如下URL。
Analysis of interactive operation examples between PHP and Web pages

  • "?"后面的内容为参数信息

  • 参数是由参数名和参数值组成的,中间使用等号“=”进行连接

  • 多个参数之间使用“&”分隔

  • username和password是参数名,对应表单中的name属性

  • test和123456是参数值,对应用户填写的内容

  • if (isset($_GET['username']) && isset($_GET['password'])) {
     echo $_GET['username']; // 输出结果:	test
     echo $_GET['password']; // 输出结果:123456
    }
    Copy after login

3.数组方式提交数据

  • 复选框是一种支持提交多个值的表单控件

  • 在编写表单时应将其 name属性设置为数组

  • <input type="checkbox" name="hobby[]" value="swimming"> 游泳
    <input type="checkbox" name="hobby[]" value="reading"> 读书
    <input type="checkbox" name="hobby[]" value="running"> 跑步
    print_r($_POST[&#39;hobby&#39;]);
    Copy after login
  • $_POST中的hobby元素是一个索引数组,数组中的元素是用户所选复选框对应的value属性值

  • 当用户未选中任何复选框时,$_POST数组中将不存在hobby元素

  • <!-- 表单控件 -->									// 接收代码
    <input type="text" name="user[name]">				$_POST[&#39;user&#39;][&#39;name&#39;];
    <input type="text" name="user[a][1]">				$_POST[&#39;user&#39;][&#39;a&#39;][1];
    <input type="text" name="user[1][b]">				$_POST[&#39;user&#39;][1][&#39;b&#39;];
    <input type="text" name="user[c][]">				$_POST[&#39;user&#39;][&#39;c&#39;][0];
    <input type="text" name="user[][d]">				$_POST[&#39;user&#39;][2][&#39;d&#39;];
    <input type="text" name="user[][]">					$_POST[&#39;user&#39;][3][0];
    <input type="text" name="user[3][][]">				$_POST[&#39;user&#39;][3][1][0];
    <input type="text" name="user[3][][]">				$_POST[&#39;user&#39;][3][2][0];
    <input type="text" name="user[][][2]">				$_POST[&#39;user&#39;][4][0][2];
    <input type="text" name="user[4][0][]">				$_POST[&#39;user&#39;][4][0][3];
    Copy after login
  • 当需要处理的表单内容非常多的情况下,表单中name属性的命名可以采用多维数组的形式,便于开发,其使用方式与PHP中的数组非常相似

  • 例如,开发在线考试系统时,表单中有填空题、单选题、多选题、判断题等多种题型,这时可以将每种题型放到一个数组里面进行提交,PHP收到后分别遍历每种题型的数组即可。

4.HTML特殊字符处理

在将用户输入的内容输出到HTML中显示时,会遇到特殊字符问题。
例如,用户提交一段HTML代码时,为了将代码原样显示,需要将里面的特殊字符串转换为实体字符,防止被浏览器解析
若没有对这些特殊字符进行处理,会给网站的安全带来风险。
为了解决这类问题,PHP提供了许多专门处理HTML特殊字符的函数
Analysis of interactive operation examples between PHP and Web pages

  • nl2br()echo nl2br(“123\n456”, false);

  • strip_tags()可以去除字符串中的标记部分,通常用于读取一段HTML代码后,去除其中的HTML标记,只保留文本。

  • 	$html = <<<&#39;EOD&#39;
    	<ul><li>苹果</li><li>香蕉</li></ul>
    	123<test>456</test><aaa>789
    	EOD;
    	echo strip_tags($html);
    	//输出结果
    	苹果香蕉
    	123456789
    Copy after login
  • 转换和还原字符串中的HTML特殊字符,
    htmlspecialchars()htmlspecialchars_decode()函数分别用于转换和还原字符串中的HTML特殊字符,具体包括“&”、单引号、双引号、“<”、“>”,其中单引号需要将函数的第2个参数设置为ENT_QUOTES常量才会进行转换。

  • $html = "123<br>4&#39;56";
    $html = htmlspecialchars($html, ENT_QUOTES | ENT_HTML5);
    echo $html, "\n";
    $str = htmlspecialchars_decode($html, 	ENT_QUOTES | ENT_HTML5);
    echo $html;
    //输出结果
    123<br>4&apos;56
    123<br>4&#39;56
    Copy after login
  • urlencode()urldecode()函数,urlencode()和urldecode()函数主要用于在HTML中输出URL参数时进行编码转换,前者用于编码,后者用于解码。
    注意,当使用$_GET接收参数时,获得的数据已经是URL解码后的结果,无需手动进行处理。

  • $name = &#39;A&B C&#39;;
    $name = urlencode($name);	// URL 编码
    echo "http://localhost/test.php?name=$name", "\n";
    echo urldecode($name);		// URL解码
    //输出结果
    http://localhost/test.php?name=A%26B+C
    A&B C
    Copy after login
  • http_build_query(),利用http_build_query()函数可以将PHP关联数组转换为URL参数字符串。

  • $params = [
    &#39;name&#39; => &#39;test&#39;,
    &#39;hobby&#39; => [&#39;reading&#39;, &#39;running&#39;]
    ];
    $query = http_build_query($params);
    echo "http://localhost/test.php?$query";
    //输出结果
    http://localhost/test.php?name=test&hobby%5B0%5D=reading&hobby%5B1%5D=running
    Copy after login

The above is the detailed content of Analysis of interactive operation examples between PHP and Web pages. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

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

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles