<&"/> <&">

Home  >  Article  >  Backend Development  >  Implementation code for PHP form verification whether the content is empty

Implementation code for PHP form verification whether the content is empty

黄舟
黄舟Original
2017-02-28 09:41:071146browse

The content is empty and the rendering is:

Implementation code for PHP form verification whether the content is empty

Fill in the content rendering:

Implementation code for PHP form verification whether the content is empty

The following is the verification procedure Code:

Implementation code for PHP form verification whether the content is empty

<!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>

The above is the content of the implementation code for PHP form verification whether the content is empty. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn