dongxian6653 2019-01-02 11:00
浏览 76
已采纳

使用Jquery和Php验证前端和后端的表单数据

i am trying do validate my form data on frontend and backend also using jquery and php using post method on jquery.

<form>
    <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
        <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
    </div>
    <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

this is my form how can i validate it on frontend and also backend without page refresh

  • 写回答

1条回答

  • duankeye2342 2019-01-02 11:11
    关注
                <script>
                $(document).ready(function(){
                  $("button").on("click", function(){
                   let email = $.trim($("#exampleInputEmail1").val());
                   let password = $.trim($("#exampleInputPassword1").val());
    
                  if(email === ""){
                   alert("Email is required!");
                   return false;
                  }
    
                 if(password === ""){
                   alert("Password is required!");
                   return false;
                 }
    
                 $.post("login.php",{email : email , password : password}, function(response){
                  response = JSON.parse(response);
                  if(response.success){
                   // redirect to homepage
                   }else{
                    alert(response.message);
                   }
                 });
                 });
                });
                </script>
    
    
        login.php
    
        <?php 
        $email = $_POST["email"];
        $password = $_POST["password"];
    
        if($email === ""){
         echo json_encode(array("success" => false,"message" => "Email is required!")); 
         exit;
        }
    
        if($password === ""){
          echo json_encode(array("success" => false,"message" => "Password is required!")); exit;
         }
    
    
    //check the record in db and send back the response
    
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用