本文章向大家介紹表單的一些知識點,然後介紹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來擷取表單資料的。
以上就是本文的全部內容,希望對大家的學習有所協助。