淺談php提交form表單

來源:互聯網
上載者:User

   這篇文章主要介紹了淺談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'; }

  Post請求把表單資料放在http請求體中,並且沒有長度限制

  form action=""意思是:form是表單,action是轉向地址,即form表單需要提交到哪裡

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.