<&"/> <&">

首頁  >  文章  >  後端開發  >  PHP表單驗證內容是否為空的實作程式碼

PHP表單驗證內容是否為空的實作程式碼

黄舟
黄舟原創
2017-02-28 09:41:071108瀏覽

內容為空效果圖為:

PHP表單驗證內容是否為空的實作程式碼

填入內容效果圖:

PHP表單驗證內容是否為空的實作程式碼
##以下是驗證程式的程式碼:


PHP表單驗證內容是否為空的實作程式碼

<!doctype html>
<html>
<head>
<meta http-equiv="conent-type" content="text/html" charset="utf-8"/>
<style>
.red{
color:red;
}
</style>
</head>
<body>
<?php
function test_input($data){
  $data=trim($data);
  $data=stripslashes($data);
  $data=htmlspecialchars($data);
  return $data;
}
?>
<?php
$name=$email=$web=$comment=$gender="";
$nameerr=$emailerr=$weberr=$commenterr=$gendererr="";
if($_SERVER[&#39;REQUEST_METHOD&#39;]=="POST"){
  if(empty($_POST[&#39;name&#39;])){
    $nameerr="必填名字";
  }else{
    $name=test_input($_POST[&#39;name&#39;]);
  }
  if(empty($_POST[&#39;email&#39;])){
    $emailerr="必填邮件";
  }else{
    $email=test_input($_POST[&#39;email&#39;]);
  }
  if(empty($_POST[&#39;web&#39;])){
    $weberr="必填网址";
  }else{
    $web=test_input($_POST[&#39;web&#39;]);
  }
  if(empty($_POST[&#39;comment&#39;])){
    $commenterr="必填备注";
  }else{
    $comment=test_input($_POST[&#39;comment&#39;]);
  }
  if(empty($_POST[&#39;gender&#39;])){
    $gendererr="必填备注";
  }else{
    $gender=test_input($_POST[&#39;gender&#39;]);
  }
}
?>
<h1>表单验证</h1>
<span class="red">*必填字段</span>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER[&#39;PHP_SELF&#39;]);?>">
名字:<input type="text" name="name"/><span class="red"><?php echo "*".$nameerr;?></span>
<br/>
E-mail:<input type="text" name="email"/><span class="red"><?php echo "*".$emailerr;?></span>
<br/>
网址:<input type="text" name="web"/><span class="red"><?php echo "*".$weberr;?></span>
<br/>
备注:<textarea rows="10" cols="40" name="comment"></textarea><span class="red"><?php echo "*".$commenterr;?></span>
<br/>
性别:<input type="radio" name="gender" value="男"/>男<input type="radio" name="gender" value="女"/>女<span class="red"><?php echo "*".$gendererr;?></span>
<br/>
<input type="submit" value="提交验证"/>
</form>
<?php
echo "名字".$name;
echo "<br/>";
echo "E-mail:".$email;
echo "<br/>";
echo "网址:".$web;
echo "<br/>";
echo "备注:".$comment;
echo "<br/>";
echo "性别:".$gender;
echo "<br/>";
?>
</body>
</html>

 以上是PHP表單驗證內容是否為空的實作程式碼 的內容,更多相關內容請關注PHP中文網(www .php.cn)!



#

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn