Heim >Backend-Entwicklung >PHP-Tutorial >PHP表单验证之必填字段--w3school学习笔记 w3school离线手册 w3school tiy w3school手册下载

PHP表单验证之必填字段--w3school学习笔记 w3school离线手册 w3school tiy w3school手册下载

WBOY
WBOYOriginal
2016-07-29 08:53:261327Durchsuche

接上文,通过PHP验证必填字段
w3school教程,w3school下载,w3school手册,w3school.com,w3school官网,w3school在线,类似w3school,离线w3school,w3school电子书,w3school在线教程,www.w3school.com,w3school.com.cn,w3school离线手册,w3school tiy,w3school手册下载
验证规则:
字段–验证规则
姓名(name)–必填,必须包含字母和空格
邮件(email)–必需,必须包含有效的电子邮件地址,包括@和.
网址(website)–可选填,如果填则必须是有效的url
评论(comment)–可选填,多行输入字段
性别(gender)–必需,选择一项

html代码:

<code><span><span>html</span>></span><span>head</span>><span>title</span>><span><span>title</span>></span><span><span>head</span>></span><span>body</span>><span>form</span><span>action</span>=<span>htmlspecialchars("<?php </span><span>echo</span> $<span>_SERVER</span>['<span>PHP_SELF</span>']; ?></span>") method='post'>
        姓名:<span>input</span><span>type</span>=<span>'text'</span><span>name</span>=<span>'name'</span> /><span>span</span><span>style</span>=<span>'color:red'</span>>*<span><span><?php </span><span>echo</span><span>$name_Err</span>; <span>?></span></span><span><span>span</span>></span><span>br</span>/></span>
        电邮:<span>input</span><span>type</span>=<span>'text'</span><span>name</span>=<span>'email'</span> /><span>span</span><span>style</span>=<span>'color:red'</span>>*<span><span><?php </span><span>echo</span><span>$email_Err</span>; <span>?></span></span><span><span>span</span>></span><span>br</span>/></span>
        网址:<span>input</span><span>type</span>=<span>'text'</span><span>name</span>=<span>'website'</span> /><span>br</span>/>
        评论:<span>textarea</span><span>name</span>=<span>'comment'</span><span>rows</span>=<span>3</span><span>cols</span>=<span>21</span>><span><span>textarea</span>></span><span>br</span>/>
        性别:<span>input</span><span>type</span>=<span>"radio"</span><span>name</span>=<span>'gender'</span><span>value</span>=<span>0</span><span>checked</span>=<span>'checked'</span>/>女性
        <span>input</span><span>type</span>=<span>"radio"</span><span>name</span>=<span>'gender'</span><span>value</span>=<span>1</span> />男性<span>br</span>/><span>input</span><span>type</span>=<span>'submit'</span><span>value</span>=<span>'提交'</span> /><span><span>form</span>></span><span><span>body</span>></span><span><span>html</span>></span></code>

php代码

<code><span><span><?php </span><span>$name</span>=<span>$email</span>=<span>$website</span>=<span>$comment</span>=<span>''</span>;
<span>$name_Err</span>=<span>$email_Err</span>=<span>''</span>;<span>//存储错误信息</span><span>if</span>(<span>$_SERVER</span>[<span>'REQUEST_METHOD'</span>]==<span>'POST'</span>){
    <span>if</span>(<span>$_POST</span>[<span>'name'</span>]==<span>''</span>){
        <span>$name_Err</span>=<span>'姓名不能为空'</span>;
    }<span>else</span>{
        <span>$name</span>=test_input(<span>$_POST</span>[<span>'name'</span>]);
    }
    <span>if</span>(<span>$_POST</span>[<span>'email'</span>]==<span>''</span>){
        <span>$email_Err</span>=<span>'邮箱不能为空'</span>;
    }<span>else</span>{
        <span>$email</span>=test_input(<span>$_POST</span>[<span>'email'</span>]);
    }
    <span>$website</span>=test_input(<span>$_POST</span>[<span>'website'</span>]);
    <span>$comment</span>=test_input(<span>$_POST</span>[<span>'comment'</span>]);
    <span>//由于我在html中将女性默认为选中,因此性别不用验证是否为空</span><span>$gender</span>=test_input(<span>$_POST</span>[<span>'gender'</span>]);
}

<span><span>function</span><span>test_input</span><span>(<span>$data</span>)</span>{</span><span>$data</span>=trim(<span>$data</span>);
    <span>$data</span>=stripslashes(<span>$data</span>);
    <span>$data</span>=htmlspecialchars(<span>$data</span>);
    <span>return</span><span>$data</span>;
}







<span>?></span></span></span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了PHP表单验证之必填字段--w3school学习笔记,包括了w3school方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
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