首页 > 编程 > PHP > 正文

浅谈php提交form表单

2020-03-22 16:44:50
字体:
来源:转载
供稿:网友

  这篇文章主要介绍了浅谈php提交form表单的2种方法和简单的示例,十分的实用,有需要的小伙伴可以参考下。

  处理GET请求

  实现的功能是输入姓名后页面显示“Hello XXX”

  创建html文件hello.html:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>欢迎</title> </head> <body> <form action="hello.php" method="get"> <input name="name" type="text"/> <input type="submit"/> </form> </body> </html>

  创建PHP文件hello.php:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/30 * Time: 15:03 */ header("Content-type: text/html; charset=utf-8"); if(isset($_GET['name'])&&$_GET['name']){//如果有值且不为空 echo 'Hello '.$_GET['name']; }else{ echo 'Please input name'; }

  Get请求把表单的数据显式地放在URI中,并且对长度和数据值编码有所限制,如:http://127.0.0.1/hello.php?name=Vito

  处理POST请求

  实现一个简单的加法运算功能

  创建html文件add.html:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>相加</title> </head> <body> <form action="add.php" method="post"> <input name="num1" type="text"/> + <input name="num2" type="text"/> <input type="submit" value="相加"/> </form> </body> </html>

  创建PHP文件add.php:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/30 * Time: 18:02 */ if($_POST['num1']&&$_POST['num2']){ echo $_POST['num1']+$_POST['num2']; }else{ echo 'Please input num'; }

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表