> 백엔드 개발 > PHP 튜토리얼 > 解析PHP中反馈表单的具体实现办法_PHP教程

解析PHP中反馈表单的具体实现办法_PHP教程

WBOY
풀어 주다: 2016-07-15 13:29:57
원래의
1053명이 탐색했습니다.

一个网站经常会出现用户注册的填写表单之类的功能。我们可以通过PHP中获取表单数据是很简单的。当一个表单被发送后,表单中所包含的各个元素被赋上了相应的值,而这样就可以像引用一般变量一样使用了。

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">FORM</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"myform"</font></span><span> </span><span class="attribute"><font color="#ff0000">ACTION</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"process_form.php3"</font></span><span> </span><span class="attribute"><font color="#ff0000">METHOD</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"POST"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span></span></li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">INPUT</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">TYPE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"TEXT"</font></span><span> </span><span class="attribute"><font color="#ff0000">NAME</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"mytext"</font></span><span> </span><span class="attribute"><font color="#ff0000">VALUE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Some Value"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">FORM</span><span class="tag">></span></font></strong><span> </span>
</li>
</ol>
로그인 후 복사

在process_form.php3中,变量$mytext就被赋予了输入的值--非常简单!同样的,你可以从列表框、多选框、单选框、按钮等表单元素中取得变量值。你唯一要做的就是为表单中的每一个元素取名以便将来可以引用。

根据这个PHP中反馈表单的方法,我们可以生成一个简单的包含三个元素的表单:姓名、e-mail地址和留言。当浏览者发送表单后,处理该表单的PHP页面(sendfdbk.php3)读取数据,检查姓名是否为空,最后将数据mail给你。

表单:form.php3

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span>   </span></span></li>
<li class="">
<span></span><span class="tag-name"><strong><font color="#006699">include</font></strong></span><span>("include/common.inc");   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">title</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"Feedback"</font></span><span>;   </span>
</li>
<li class=""><span>include("include/header.inc");   </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">P</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">FORM</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">ACTION</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"sendfdbk.php3"</font></span><span> </span><span class="attribute"><font color="#ff0000">METHOD</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"POST"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">INPUT</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">TYPE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span> </span><span class="attribute"><font color="#ff0000">NAME</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"name"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Your name"</font></span><span> </span><span class="attribute"><font color="#ff0000">SIZE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"20"</font></span><span> </span><span class="attribute"><font color="#ff0000">MAXLENGTH</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"30"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">INPUT</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">TYPE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span> </span><span class="attribute"><font color="#ff0000">MAXLENGTH</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"40"</font></span><span> </span><span class="attribute"><font color="#ff0000">WIDTH</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"20"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Your Email"</font></span><span> </span><span class="attribute"><font color="#ff0000">NAME</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"email"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">BR</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">TEXTAREA</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">ROWS</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"7"</font></span><span> </span><span class="attribute"><font color="#ff0000">COLS</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"40"</font></span><span> </span><span class="attribute"><font color="#ff0000">NAME</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"comment"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span>
</li>
<li class=""><span>Your feedback on my home page.   </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">TEXTAREA</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">BR</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">INPUT</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">TYPE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"submit"</font></span><span> </span><span class="attribute"><font color="#ff0000">VALUE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Send Feedback!"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">FORM</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">P</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span>   </span>
</li>
<li class="alt">
<span></span><span class="tag-name"><strong><font color="#006699">include</font></strong></span><span>("include/footer.inc");   </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
</ol>
로그인 후 복사

PHP中反馈表单之处理表单:sendfdbk.php3

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span>   </span></span></li>
<li class="">
<span></span><span class="tag-name"><strong><font color="#006699">include</font></strong></span><span>("include/common.inc");   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">title</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"Feedback"</font></span><span>;   </span>
</li>
<li class=""><span>include("include/header.inc");   </span></li>
<li class="alt">
<span>if ( $</span><span class="attribute"><font color="#ff0000">name</font></span><span> == "" )   </span>
</li>
<li class=""><span>{   </span></li>
<li class="alt"><span>// 现在我很讨厌匿名的留言!   </span></li>
<li class=""><span>echo "Duh ? How come you are anonymous?";   </span></li>
<li class="alt"><span>}   </span></li>
<li class="">
<span>elseif ($</span><span class="attribute"><font color="#ff0000">name</font></span><span> == "Your name")   </span>
</li>
<li class="alt"><span>{   </span></li>
<li class=""><span>// 这个浏览者真是不想透露姓名啊!   </span></li>
<li class="alt">
<span>echo "Hello ? </span><strong><font color="#006699"><span class="tag"><span class="tag-name">B</span><span class="tag">></span></span></font></strong><span>Your name</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">B</span><span class="tag">></span></font></strong><span> is supposed to be replaced with   </span>
</li>
<li class="">
<span>your actual name!</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">B</span><span class="tag">></span></font></strong><span>";   </span>
</li>
<li class="alt"><span>}   </span></li>
<li class=""><span>else   </span></li>
<li class="alt"><span>{   </span></li>
<li class=""><span>// 输出一段礼貌的感谢语   </span></li>
<li class="alt"><span>echo "   </span></li>
<li class=""><span>Hello, $name.   </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">BR</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class=""><span>Thank you for your feedback. It is greatly appreciated.   </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">BR</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class=""><span>Thanking you   </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">BR</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class="">
<span>$MyName </span><strong><font color="#006699"><span class="tag"><span class="tag-name">BR</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class="alt"><span>$MyEmailLink   </span></li>
<li class=""><span>";   </span></li>
<li class="alt"><span>// 最后mail出去   </span></li>
<li class=""><span>mail($MyEmail, "Feedback.","   </span></li>
<li class="alt"><span>Name : $name   </span></li>
<li class=""><span>E-mail : $email   </span></li>
<li class="alt"><span>Comment : $comment   </span></li>
<li class=""><span>");   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span>include("include/footer.inc");   </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
</ol>
로그인 후 복사

上面这段代码就是PHP中反馈表单的全部实现方式。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446343.htmlTechArticle一个网站经常会出现用户注册的填写表单之类的功能。我们可以通过 PHP中获取表单数据是很简单的。当一个表单被发送后,表单中所包含的...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿