如何在不重新加载页面的情况下显示弹出消息?

时间:2018-10-05 20:56:34

标签: php laravel

我想在弹出表单上显示消息而无需重新加载页面,但是当我按下按钮时,它将重新加载页面并且没有消息显示。

我的控制器:

$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 response()->json([
        'success_message' => 'password has been changed successfully',
    ], 422);
} else {
    return response()->json([
        'modal_message_danger' => 'wrong old password'
    ], 422);
}

我的Ajax:

$('#password_change_form').submit(function(e) {
   e.preventDefault();
   var saveThis = this;
   $.ajax({
     type: "POST",
     url: "/changepassword",
     data: $(saveThis).serialize(),
     success: function(data) {
        alert(data);
     }
   });
});

但是它什么也没做; Ajax无法正常工作。我要显示消息。

currently it's showing like this

this is my popup form where I want to display my message

1 个答案:

答案 0 :(得分:0)

要获取jquery对象,我认为您需要更改

var saveThis = this;

var saveThis = $(this);