PHP与Web页面交互操作实例分析

吾爱主题 阅读:87 2021-10-16 16:24:00 评论:0

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

web交互

1.web表单交互

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

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

  • 常见的超全局变量数组变量有$ _post、$_get等
  • 通过post方式提交的数据会保存到$_post中
  • 通过get方式提交的数据会保存到$_get中

2.url参数交互

当表单以get方式提交时,会将用户填写的内容放在url参数中进行提交。
表单的method属性删除(或将其值改为get),然后提交表单,会得到如下url。

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

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

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

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

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

    ?
    1 2 3 4 if (isset( $_get [ 'username' ]) && isset( $_get [ 'password' ])) {   echo $_get [ 'username' ]; // 输出结果:   test   echo $_get [ 'password' ]; // 输出结果:123456 }

3.数组方式提交数据

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

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

    ?
    1 2 3 4 <input type= "checkbox" name= "hobby[]" value= "swimming" > 游泳 <input type= "checkbox" name= "hobby[]" value= "reading" > 读书 <input type= "checkbox" name= "hobby[]" value= "running" > 跑步 print_r( $_post [ 'hobby' ]);
  • $_post中的hobby元素是一个索引数组,数组中的元素是用户所选复选框对应的value属性值

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

    ?
    1 2 3 4 5 6 7 8 9 10 11 <!-- 表单控件 -->                                 // 接收代码 <input type= "text" name= "user[name]" >             $_post [ 'user' ][ 'name' ]; <input type= "text" name= "user[a][1]" >             $_post [ 'user' ][ 'a' ][1]; <input type= "text" name= "user[1][b]" >             $_post [ 'user' ][1][ 'b' ]; <input type= "text" name= "user[c][]" >              $_post [ 'user' ][ 'c' ][0]; <input type= "text" name= "user[][d]" >              $_post [ 'user' ][2][ 'd' ]; <input type= "text" name= "user[][]" >                   $_post [ 'user' ][3][0]; <input type= "text" name= "user[3][][]" >                $_post [ 'user' ][3][1][0]; <input type= "text" name= "user[3][][]" >                $_post [ 'user' ][3][2][0]; <input type= "text" name= "user[][][2]" >                $_post [ 'user' ][4][0][2]; <input type= "text" name= "user[4][0][]" >               $_post [ 'user' ][4][0][3];
  • 当需要处理的表单内容非常多的情况下,表单中name属性的命名可以采用多维数组的形式,便于开发,其使用方式与php中的数组非常相似

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

4.html特殊字符处理

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

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

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

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

    ?
    1 2 3 4 5 6 7 8 $html = "123<br>4'56" ; $html = htmlspecialchars( $html , ent_quotes | ent_html5); echo $html , "\n" ; $str = htmlspecialchars_decode( $html ,   ent_quotes | ent_html5); echo $html ; //输出结果 123&lt;br&gt;4&apos;56 123<br>4'56
  • urlencode()和urldecode()函数,urlencode()和urldecode()函数主要用于在html中输出url参数时进行编码转换,前者用于编码,后者用于解码。
    注意,当使用$_get接收参数时,获得的数据已经是url解码后的结果,无需手动进行处理。

    ?
    1 2 3 4 5 6 7 $name = 'a&b c' ; $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
  • http_build_query(),利用http_build_query()函数可以将php关联数组转换为url参数字符串。

    ?
    1 2 3 4 5 6 7 8 $params = [ 'name' => 'test' , 'hobby' => [ 'reading' , 'running' ] ]; $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

希望本文所述对大家PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_41254184/article/details/89035002

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容