위에서 계속해서 PHP를 통해 필수 필드를 확인하세요.
유효성 검사 규칙:
필드 - 유효성 검사 규칙
이름 – 필수, 문자와 공백을 포함해야 합니다
이메일 – 필수, @ 및
를 포함한 유효한 이메일 주소가 포함되어 있어야 합니다.
URL(웹사이트) - 선택 사항입니다. 입력한 경우 유효한 URL이어야 합니다.
댓글 – 선택 사항, 여러 줄 입력 필드
성별 – 필수, 하나 선택
html 코드:
<code><span><!DOCTYPE html></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>title</span>></span><span></<span>title</span>></span><span></<span>head</span>></span><span><<span>body</span>></span><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><<span>input</span><span>type</span>=<span>'text'</span><span>name</span>=<span>'name'</span> /></span><span><<span>span</span><span>style</span>=<span>'color:red'</span>></span>*<span><span><?php</span><span>echo</span><span>$name_Err</span>; <span>?></span></span><span></<span>span</span>></span><span><<span>br</span>/></span> 电邮:<span><<span>input</span><span>type</span>=<span>'text'</span><span>name</span>=<span>'email'</span> /></span><span><<span>span</span><span>style</span>=<span>'color:red'</span>></span>*<span><span><?php</span><span>echo</span><span>$email_Err</span>; <span>?></span></span><span></<span>span</span>></span><span><<span>br</span>/></span> 网址:<span><<span>input</span><span>type</span>=<span>'text'</span><span>name</span>=<span>'website'</span> /></span><span><<span>br</span>/></span> 评论:<span><<span>textarea</span><span>name</span>=<span>'comment'</span><span>rows</span>=<span>3</span><span>cols</span>=<span>21</span>></span><span></<span>textarea</span>></span><span><<span>br</span>/></span> 性别:<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>女性 <span><<span>input</span><span>type</span>=<span>"radio"</span><span>name</span>=<span>'gender'</span><span>value</span>=<span>1</span> /></span>男性<span><<span>br</span>/></span><span><<span>input</span><span>type</span>=<span>'submit'</span><span>value</span>=<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></code>').addClass('pre-numbering') .hide() ; $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
위 내용은 w3school 콘텐츠를 포함하여 PHP 양식 검증에 필요한 필드를 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.