php - 如何在 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 请求有什么问题。您的帮助将不胜感激!
在此先感谢您需要您的帮助。
解决方案
$("#myform").submit(function(e){
e.preventDefault();
//put your ajax here
});
您需要使用上面的代码阻止表单提交。
推荐阅读
- html - 从 Express NodeJS 中的选中复选框中获取所有“名称”属性
- angular - 角表排序,没有角材料
- python - Python 正则表达式:从字符串中提取体积(mL)
- java - 在其他应用程序之上创建按钮并使用 OnClickListener 的服务
- c# - 在进行 WCF https 调用时,Flutter Web 出现“XMLHttpRequest”错误
- php - 在 FilesystemManager 中找不到 Laravel 8 Class League AwsS3Adapter
- java - 在创建自定义处理器 apache nifi 期间出错
- python - Python中是否有一个函数可以在具有约束的数据结构中插入一个元素
- java - 在 JButton 操作上多次替换 JLabel 文本
- java - 没有得到输出。样本输入:7 & {1,2,1,2,1,3,2} 样本输出:2. 找出可能的对总数