這篇文章主要介紹了淺談php提交form表單的2種方法和簡單的示例,十分的實用,有需要的小伙伴可以參考下。
處理GET請求
實現的功能是輸入姓名后頁面顯示“Hello XXX”
創建html文件hello.html:
- <!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:
- <?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:
- <!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:
- <?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';
- }
Post請求把表單數據放在http請求體中,并且沒有長度限制
form action=""意思是:form是表單,action是轉向地址,即form表單需要提交到哪里
以上所述就是本文的全部內容了,希望大家能夠喜歡。
新聞熱點
疑難解答