博客列表 >php 粘性表单验证

php 粘性表单验证

小的博客
小的博客原创
2017年12月28日 10:17:481236浏览
<!DOCTYPE html><html> 
  <head>
      <title>粘性表单</title>  
          <link rel="stylesheet" href="dist/css/bootstrap.css">
          <script src=" https://code.jquery.com/jquery-3.2.1.min.js">
          </script><script src="dist/js/bootstrap.js"></script>
   </head> 
   <body>    
       <form action='' method="post">   //建立表单   
       <p>
             <label>用户名:
                              <input type="text" name="user_name" value=<?php echo (isset                              ($_POST['user_name']))?$_POST['user_name']:''?>>
             </label> 
        </p>  
        <p> 
             <label>密&nbsp;&nbsp;&nbsp;码:
     <input type="password" name="password" value=<?php echo (isset($_POST['password']))?$_POST['password']:''?>>
             </label>
         </p>  
         <p>
           <label>邮&nbsp;&nbsp;&nbsp;箱:
           <input type="email" name="e_mail" value=<?php echo (isset($_POST['e_mail']))?$_POST['e_mail']:''?>>
           </label>
          </p>   
          <label>性&nbsp;&nbsp;&nbsp;别:<input type="radio" name="gender" value='male'<?php echo (isset($_POST['gender']) && $_POST['gender']=='male')?'checked':''?>>男                   <input type="radio" name="gender" value="woman"<?php echo (isset($_POST['gender']) && $_POST['gender']=='woman')?'checked':''?>>女
          </label>  
           <select name="degree">     
                <h5>学历</h5>        
                <option>请选择</option>     
                <option value="bk"<?php echo (isset($_POST['degree']) && $_POST['degree']=='bk')?'selected':''?>>本科</option>     
                <option value="yjs"<?php echo (isset($_POST['degree']) && $_POST['degree']=='yjs')?'selected':''?>>研究生</option>     
                <option value="bs"<?php echo (isset($_POST['degree']) && $_POST['degree']=='bs')?'selected':''?>>博士</option>   
             </select>   
             <input type="submit" value="提交">   
   </form>  
  </body>
  </html>
  /*下面一段PHP代码进行验证
  <?php
    if($_SERVER['REQUEST_METHOD']){ /*判断是否是post方式提交过来的数据
    $name=isset($_POST['user_name'])?$_POST['user_name']:null; 
    /*用户名验证,不能为空,不能少于六位大于十位
    if(empty($name)){  
        echo '<script>alert("请输入用户名")</script>';  
          exit; 
     }elseif(strlen($name)<6){
       echo '<script>alert("用户名不能少于六位")</script>';  
       exit; 
     }elseif(strlen($name)>10){  
        echo '<script>alert("用户名不能大于十位")</script>';  
        exit; 
     }else{  
            echo $name.'<br>'; 
          } 
     /*密码验证:密码不能少于六位不能大于十位不能为空
    $pass=isset($_POST['password'])?$_POST['password']:null; 
    if(empty($pass)){ 
        echo '<script>alert("密码不能为空")</script>'; 
         exit;
     }elseif(strlen($pass)<6){
         echo '<script>alert("密码不能小于六位")</script>'; 
         exit; 
     }elseif(strlen($pass)>10){  
          echo '<script>alert("密码不能大于十位")</script>'; 
          exit;
     }else{ 
      echo $pass.'<br>'; 
    }
    //邮箱验证:邮箱格式必须正确,邮箱不能为空
   $email=isset($_POST['e_mail'])?$_POST['e_mail']:null; 
   if(empty($email)){  
       echo '<script>alert("电子邮件不能为空")</script>';
   }elseif(!preg_match("/[\w\-]+\@[\w\-]+\.[\w\-]+/",$email)){  
       echo '<script>alert("电子邮件不合法")</script>'; 
   }else{  
       echo $email.'<br>';
      } 
      //单选按钮性别验证
  $gender=isset($_POST['gender'])?$_POST['gender']:null;  
     if(empty($gender)){ 
      echo '<script>alert("请选择性别")</script>'; 
    }elseif($gender=='male'){  
        echo '性别是男</br>';
    }elseif($gender=='woman'){ 
        echo '性别是女<br>';
      }
      /*下拉框学历验证
    $degre=isset($_POST['degree'])?$_POST['degree']:null; 
    if(empty($degre)){ 
        echo '<script>alert("请选择学历")</script>';   
    }elseif($degre=='bk'){ 
       echo '学历是本科<br>';
    }elseif($degre=='yjs'){
        echo '学历是研究生<br>';   
    }elseif($degre=='bs'){
        echo '学历是博士<br>';   
    }    
    echo '<script>confirm("确认提交吗?");</script>'; 
    echo $name.'<br>'; echo $pass.'<br>'; echo $email.'<br>'; echo $gender.'<br>'; echo $degre.'<br>';
    }?>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议