【问题标题】:How to submit a popup form with ajax request in laravel如何在 laravel 中提交带有 ajax 请求的弹出表单
【发布时间】:2018-10-05 14:43:23
【问题描述】:

我的 Ajax 代码

            Query(document).ready(function(){
            jQuery('#password_form').click(function(){
           $.ajaxSetup({
              headers: {
                  'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
              }
          });
           jQuery.ajax({
              url: "{{ url('/changepassword') }}",
              method: 'post',
              data: {
                 password: jQuery('#password').val(),
                 new_password: jQuery('#new_password').val(),
                 password_confirmation: jQuery('#password_confirmation').val()
              },
              success: function(result){
                 console.log(result);
              }});
           });
        }); 

我的控制器:

         public function changepassword(Request $request){
    $user = Auth::guard()->user();
    $request_data = $request->All();
    $validator = $this->admin_credential_rules($request_data);
    if($validator->fails()) {
          $errors = $validator->errors();
        $errors = json_decode($errors);

        return response()->json([
            'success' => false,
            'message' => $errors
        ], 422);            } else {

        $current_password = $user->password;
        if(md5($request_data['password']) == $current_password) {
            $user_id = $user->id;
            $obj_user = User::find($user_id);
            $obj_user->password = md5($request_data['new_password']);
            $obj_user->save();

             return \Illuminate\Support\Facades\Redirect::to('mujucet')
                ->with("modal_message_success", "Password has been changed successfully");
        } else {
            return \Illuminate\Support\Facades\Redirect::to('mujucet')
                ->with("modal_message_danger", "wong old password");           
     }
    }
}

我有一个弹出窗口,有三个字段 1-密码 2- 新密码 3- 密码确认

在 ajax 之前,我的表单正在提交,但我想用 ajax 提交表单,所以我的页面不应该重新加载,我的成功和错误消息应该显示在我的弹出表单上,但是当我点击按钮时,它的重新加载和值也不是已提交。

我不知道我的 ajax 请求有什么问题。 非常感谢您的帮助!

在此先感谢您需要您的帮助。

【问题讨论】:

  • 你的错误是什么?

标签: php laravel popup


【解决方案1】:
 $("#myform").submit(function(e){
    e.preventDefault();
    //put your ajax here
 });

您需要使用上面的代码阻止表单提交。

【讨论】:

    【解决方案2】:

    您的弹出提交按钮应该是按钮不提交

    <button type="button" class="btn btn-primary" title="" 
    id="btn_submit">add</button>
    
    <script>
    $('#btn_submit').click(function () {
    var type = $('#contain-type').val(); // take your all values you want to send 
    
    /*ajax call*/
    $.post(baseUrl + '/admin/confirmation-mail(your route)',{"_token": "{{ 
    csrf_token() }}", id: parameter, subject: parameter},
    function (data, status) { 
    alert(data);
    });
    )};
    </script>
    

    控制器

    public function name(Request $request){
    dd(request->all()); //add your php code
    }
    

    【讨论】:

    • 嘿,你在吗@shika?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2018-03-10
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多