PHP与Web页面的交互示例详解二

吾爱主题 阅读:176 2021-10-21 12:39:00 评论:0

前言

在《php学习笔记-php与web页面的交互1》笔记中讲解了form表单的一些属性,包括它的输入域标记、选择域标记和文字域标记的写法,接下来的内容就是讲如何获取表单数据以及php数据的传递,包括对各种控件值的获取。

插入表单

提交表单之前一定得有表单,当我们的表单创建完毕后可以将表单插入web页中,代码如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" > <html> <head> <meta http-equiv= "content-type" content= "text/html; charset=gb2312" > <title>在普通的web页中插入表单</title> <style type= "text/css" > body,td,th {    font-size: 12px; } </style> </head> <body> <form action= "demo_1.php" method= "post" name= "form1" enctype= "multipart/form-data" >   <table width= "405" height= "24" border= "1" cellpadding= "1" cellspacing= "1" bordercolor= "#ffffff" bgcolor= "#999999" >    <tr bgcolor= "#ffcc33" >     <td width= "103" height= "25" align= "right" >商品名称:</td>     <td height= "25" align= "left" ><input name= "product" type= "text" id= "user" size= "20" maxlength= "100" ></td>    </tr>    <tr bgcolor= "#ffcc33" >     <td height= "25" align= "right" >市场:</td>     <td height= "25" colspan= "2" align= "left" ><input name= "from" type= "radio" value= "海外" checked>      海外      <input type= "radio" name= "from" value= "国内" >      国内</td>    </tr>    <tr bgcolor= "#ffcc33" >     <td width= "103" height= "25" align= "right" >编号:</td>     <td width= "289" height= "25" colspan= "2" align= "left" ><input name= "code" type= "text" id= "code" size= "20" maxlength= "100" ></td>    </tr>    <tr bgcolor= "#ffcc33" >     <td height= "25" align= "right" >种类:</td>     <td height= "25" colspan= "2" align= "left" ><select name= "select" >       <option value= "电器" >电器</option>       <option value= "家具" >家具</option>       <option value= "化妆品" >化妆品</option>       <option value= "图书" selected>图书</option>       <option value= "服饰" >服饰</option>       <option value= "宠物" >宠物</option>       <option value= "计算机" >计算机</option>      </select></td>    </tr>    <tr bgcolor= "#ffcc33" >     <td height= "25" align= "right" >商品图片: </td>     <td height= "25" colspan= "2" align= "left" ><input name= "photo" type= "file" size= "20" maxlength= "1000" id= "photo" ></td>    </tr>    <tr bgcolor= "#ffcc33" >     <td height= "25" align= "right" >商品描述: </td>     <td height= "25" colspan= "2" align= "left" ><textarea name= "intro" cols= "28" rows= "3" id= "info" ></textarea></td>    </tr>    <tr align= "center" bgcolor= "#ffcc33" >     <td height= "25" colspan= "3" ><input type= "submit" name= "submit" value= "提交" >      &nbsp;&nbsp;      <input type= "reset" name= "submit2" value= "重置" ></td>    </tr>   </table> </form> <?php header( "content-type:text/html;  charset=gb2312" ); ?>> </body> </html>

在html的<body>和</body>间添加一个表单。

运行结果:

获取表单数据

表单数据的获取主要有两个钟方式分别是post()方法和get()方法。

通过<form>表单的method属性所指定。

使用post方法提交表单

应用post方法时,只需将<form>表单中的属性method设置成post即可。post方法不依赖于url,不会显示在地址栏。post方法可以没有限制地传递数据到服务器,所有提交的信息在后台传输,用户在浏览器端是看不到这一过程的,安全性高。所以post方法比较适合用于发送一个保密的(如信用卡号)或者容量较大的数据到服务器。

示例:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"    "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head>    <meta http-equiv= "content-type" content= "text/html; charset=gb2312"      />    <title>php语言基础</title>   </head> <body> <form action= "index.php" method= "post" name= "form1" enctype= "multipart/form-data" >    <table width= "300" border= "1" cellpadding= "10" cellspacing= "0" >      <tr>        <td height= "30" >编号:          <input type= "text" name= "code" size= "20" />          <input type= "submit" name= "subimt" value= "提交" />        </td>      </tr>    </table> </form> <?php header( "content-type:text/html;  charset=gb2312" ); ?> </body> </html>

运行结果:

点击提交按钮后,我们发现地址栏不会显示我们提交的参数。

使用get方法提交表单

get方法是<form>表单中method属性的默认方法。使用get方法提交的表单数据被附加到url后,并作为url的一部分发送到服务器端。在程序的开发过程中,由于get方法提交的数据是附加到url上发送的,因此,在url的地址栏中将会显示“url+用户传递的参数”。

将上面示例中的<form>表单中method属性指定为get,运行程序后的结果如下:

点击按钮后地址栏会通过‘?'连接键值对,键值对以‘&'分隔。

php参数传递的常用方法

获取表单数据,实际上就是获取不同的表单元素的数据。<form>标签中的name是所有表单元素都具备的属性,即为这个表单元素的名称,在使用时需要使用name属性来获取相应的value属性值。

php参数传递的常用方法有三种:

  1. $_post[]全局变量
  2.  $_get[]全局变量
  3. $_session[]变量

$_post[]全局变量

使用php的$_post[]预定义变量可以获取表单元素的值,格式为:

?
1 $_post [name]

范例:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"    "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head>    <meta http-equiv= "content-type" content= "text/html; charset=gb2312"      />    <title>php语言基础</title>   </head> <body> <form action= "index.php" method= "post" name= "form1" enctype= "multipart/form-data" >    <table width= "300" border= "1" cellpadding= "10" cellspacing= "0" >      <tr>        <td height= "30" >编号:          <input type= "text" name= "code" size= "20" />          <input type= "submit" name= "subimt" value= "提交" />        </td>      </tr>    </table> </form> <?php header( "content-type:text/html;  charset=gb2312" ); $value = $_post [ 'code' ]; echo "编号:" . $value ; ?> </body> </html>

运行结果:

$_get[]全局变量

php使用$_get[]预定义变量获取通过get方法传过来的值,使用格式为:

?
1 $_get [name]

范例:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"    "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head>    <meta http-equiv= "content-type" content= "text/html; charset=gb2312"      />    <title>php语言基础</title>   </head> <body> <form action= "index.php" method= "get" name= "form1" enctype= "multipart/form-data" >    <table width= "300" border= "1" cellpadding= "10" cellspacing= "0" >      <tr>        <td height= "30" >编号:          <input type= "text" name= "code" size= "20" />          <input type= "submit" name= "subimt" value= "提交" />        </td>      </tr>    </table> </form> <?php header( "content-type:text/html;  charset=gb2312" ); $value = $_get [ 'code' ]; echo "编号:" . $value ; ?> </body> </html>

运行结果:

$_session[]变量

使用$_session[]变量可以获取表单元素的值,格式为:

?
1 $_session [name]

使用$_session[]传参的方法获取的变量值,保存之后任何页面都可以使用。但这种方法很耗费系统资源,建议读者慎重使用。

案例

最后结合笔记中的第一个demo写一个完整的案例:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" > <html> <head>    <meta http-equiv= "content-type" content= "text/html; charset=gb2312" >    <title>在普通的web页中插入表单</title>    <style type= "text/css" >      body, td, th {        font-size: 12px;      }    </style> </head> <body> <form action= "demo_1.php" method= "post" name= "form1" enctype= "multipart/form-data" >    <table width= "405" height= "24" border= "1" cellpadding= "1" cellspacing= "1" bordercolor= "#ffffff" bgcolor= "#999999" >      <tr bgcolor= "#ffcc33" >        <td width= "103" height= "25" align= "right" >商品名称:</td>        <td height= "25" align= "left" ><input name= "product" type= "text" id= "user" size= "20" maxlength= "100" ></td>      </tr>      <tr bgcolor= "#ffcc33" >        <td height= "25" align= "right" >市场:</td>        <td height= "25" colspan= "2" align= "left" ><input name= "from" type= "radio" value= "海外" checked>          海外          <input type= "radio" name= "from" value= "国内" >          国内        </td>      </tr>      <tr bgcolor= "#ffcc33" >        <td width= "103" height= "25" align= "right" >编号:</td>        <td width= "289" height= "25" colspan= "2" align= "left" ><input name= "code" type= "text" id= "code" size= "20"                                      maxlength= "100" ></td>      </tr>      <tr bgcolor= "#ffcc33" >        <td height= "25" align= "right" >种类:</td>        <td height= "25" colspan= "2" align= "left" ><select name= "select" >            <option value= "电器" >电器</option>            <option value= "家具" >家具</option>            <option value= "化妆品" >化妆品</option>            <option value= "图书" selected>图书</option>            <option value= "服饰" >服饰</option>            <option value= "宠物" >宠物</option>            <option value= "计算机" >计算机</option>          </select></td>      </tr>      <tr bgcolor= "#ffcc33" >        <td height= "25" align= "right" >商品图片:</td>        <td height= "25" colspan= "2" align= "left" ><input name= "photo" type= "file" size= "20" maxlength= "1000"                                id= "photo" ></td>      </tr>      <tr bgcolor= "#ffcc33" >        <td height= "25" align= "right" >商品描述:</td>        <td height= "25" colspan= "2" align= "left" ><textarea name= "intro" cols= "28" rows= "3" id= "info" ></textarea>        </td>      </tr>      <tr align= "center" bgcolor= "#ffcc33" >        <td height= "25" colspan= "3" ><input type= "submit" name= "submit" value= "提交" >          &nbsp;&nbsp;          <input type= "reset" name= "submit2" value= "重置" ></td>      </tr>    </table> </form> <?php header( "content-type:text/html;  charset=gb2312" );   if ( $_post [submit] != "" ) {    echo "商品清单:" ;    echo " <br><br>商品名称:" . $_post [ 'product' ];    echo " <br><br>  市场:" . $_post [from];    echo " <br><br>  编号:" . $_post [ 'code' ];    echo " <br><br>  种类:" . $_post [ 'select' ];    $path = './upfiles/' . $_files [ 'photo' ][ 'name' ];    move_uploaded_file( $_files [ 'photo' ][ 'tmp_name' ], $path );    echo " <br><br>商品图片:" . $path ;    echo " <br><br>商品描述:" . $_post [ 'intro' ]; }     ?> </body> </html>

运行结果:

通过move_uploaded_file方法将图片上传到当前路径下的upfiles文件夹中。

到此这篇关于php与web页面的交互示例详解二的文章就介绍到这了,更多相关php与web页面的交互内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/hai_qing_xu_kong/article/details/51761308

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

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

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

    了解等多精彩内容