Heim > Backend-Entwicklung > PHP-Tutorial > 解析PHP中反馈表单的具体实现办法_PHP教程

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

WBOY
Freigeben: 2016-07-15 13:29:57
Original
1055 Leute haben es durchsucht

一个网站经常会出现用户注册的填写表单之类的功能。我们可以通过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>
Nach dem Login kopieren

在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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446343.htmlTechArticle一个网站经常会出现用户注册的填写表单之类的功能。我们可以通过 PHP中获取表单数据是很简单的。当一个表单被发送后,表单中所包含的...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage