繁体   English   中英

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

[英]Laravel Submit Form Without Page Refresh Using Ajax

我想在我的模态弹出表单中发布电话号码。但不想在点击“继续”按钮提交表单后消失弹出窗口。 这是我的刀片。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>

这是我的 controller function

     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);
               } 

使用类型=按钮

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

并在点击事件上触发您的 ajax

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM