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

使用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 python动态规划:N根火柴摆出的最大数字
  • ¥20 (标签-excel)
  • ¥200 求idea10和MyEclipse7.1
  • ¥20 vb6.0截取当前窗体保存为jpg文件
  • ¥20 苹果手机不使用大疆sdk怎么获取遥控器控制信息或如何接入大疆sdk并且成功上架sdk
  • ¥20 woocommerce 注册按键重定向
  • ¥100 求书法图像文字切割代码
  • ¥15 同一个波形探测距离不同的目标,为什么fft之后得到的频谱图会发生移动,不应该时移不改变幅度谱吗(标签-matlab)(相关搜索:matlab仿真)
  • ¥15 Proteus仿真程序只能执行一次
  • ¥15 语音识别websocket报错