首页 > 解决方案 > 如何在 laravel 中提交带有 ajax 请求的弹出表单

问题描述

我的 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- new_password 3- password_confirmation

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

我不知道我的 ajax 请求有什么问题。您的帮助将不胜感激!

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

标签: phplaravelpopup

解决方案


 $("#myform").submit(function(e){
    e.preventDefault();
    //put your ajax here
 });

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


推荐阅读