当前位置:首页 > PHP教程 > 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.0rn"; $headers .= "content-type: text/html; charset=iso-8859-1rn"; $headers .= "from: $useraddr rn"; $headers .= "cc: another@hotmail.com rn"; mail( $to, $re, $msg, $headers ); ?>

因为表单是以post方式提交,所以这里是使用$_post来获取表单数据的。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!