提问者:小点点

Laravel使用Ajax提交表单而不刷新页面


我想在我的模式弹出表单中张贴电话号码。但不想在提交表单后单击“继续”按钮消失弹出。 下面是我的blade.php代码。

            <div id="modal-wrapper" class="modal">
  
              <form class="modal-content animate" id="addform">
               
                               
                    <input type="text" class="form-control" name="phone_number" placeholder="01**********">
                  </div> 
                  </div>
                  
                <div style="text-align: center; margin-top: 10px;padding-left:18px;">
                  <button type="submit"  style="background-color: #a5abb0;" class="mobile-login__button">CONTINUE
                </button>
              </div>

下面是我的ajax脚本部分代码的一部分。

 <script>
                
                  $("#addform").on('submit',function(e){
                    e.preventDefault();
              
                    $.ajax({
                      type:"post"
                      url: "/client-mobile-login"
                      data: $("addform").serialize(),
                      success:function(response){
                        
                      },
                      error:function(error){
                        console.log(error)
                        alert("not send");
                      }
              
                      )};
                    });
                  
                });
              
                </script>

这是我的控制器函数

     public function client_mobile_login(Request $request)
      {
       $client_phone1='88'.$request->input('phone_number');
    
    $result=DB::table('client')
            ->where('client_phone1',$client_phone1)
            ->first();
           {{ here is my otp sending code.... }}

            if($result)
            {       
                   $request->session()->put('client_title', $result->client_title);
                   

               // print_r($client_phone1);
               } 

共1个答案

匿名用户

使用type=button

<button type="button" id="submitForm" ...>CONTINUE</button>

并触发ajax on click事件

$("#submitForm").on('click',function(e){ ... }