PHP 表單提交及處理表單資料詳解

來源:互聯網
上載者:User
本文章向大家介紹表單的一些知識點,然後介紹PHP是如何接收表單資料並如何處理表單資料,文章以一個發送郵件的表單一實例來向大家講解表單提交及php如何處理表單資料,需要的朋友可以參考下

先來看一下html form表單的源碼:

<html> <head> <title>Feedback Form</title> </head> <body> <form action="feedback.php" method="post"> Name:<input type="text" name="username" size="30"> <br><br> Email:<input type="text" name="useraddr" size="30"> <br><br> <textarea name="comments" cols="30" rows="5"> </textarea><br> <input type="submit" value="Send Form"> </form> </body></html>

表單是以<form>開頭,以</form>結束。

action表示要將表單提交到哪個檔案進行處理資料,這裡是提交到feedback.php檔案進行處理表單資料。

method表示以何種方式提交表單,一般有兩種方式提交表單,post方式和get方式。get方式提交表單,資料會顯示在url連結上,post方式提交表單,資料是隱藏的,不會顯示在url連結上。

在這個執行個體中,有很多html input標籤,這些標籤都是表單元素。

php處理表單資料的代碼如下:

<?php$username = $_POST['username'];$useraddr = $_POST['useraddr'];$comments = $_POST['comments'];$to = "php@h.com"; $re = "Website Feedback";     $msg = $comments;      $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";$headers .= "From: $useraddr \r\n";  $headers .= "Cc: another@hotmail.com \r\n";mail( $to, $re, $msg, $headers );   ?>

因為表單是以post方式提交,所以這裡是使用$_POST來擷取表單資料的。

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.