首页 > 开发 > PHP > 正文

浅谈php提交form表单

2024-05-04 23:37:02
字体:大 中 小
来源:转载
供稿:网友

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

处理GET请求

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

创建html文件hello.html:

 

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

创建PHP文件hello.php:

 

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

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

处理POST请求

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

创建html文件add.html:

 

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

创建PHP文件add.php:

 

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

Post请求把表单数据放在http请求体中,并且没有长度限制

form action=""意思是:form是表单,action是转向地址,即form表单需要提交到哪里

以上所述就是本文的全部内容了,希望大家能够喜欢。

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