简体   繁体   中英

Laravel Submit Form Without Page Refresh Using Ajax

I want to post the phone number in my modal popup form.but don't want to disappear the popup after submitting the form clicking on "continue" button. Here is my blade.php code.

            <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>

here is my script part for ajax part of the code.

 <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>

and here is my 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);
               } 

Use type=button

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

And trigger your ajax on click event

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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