[英]How can I display a flash message using ajax after I reload the laravel view
I have a form in my laravel view. 我在laravel视图中有一个表单。 When I click on sendotp button, first there is database validation is done to check if that number already exists in the database or not, then accordingly it will return 0 or 1. If the number already exists then ie I receive 1 from the controller validation then I want to reload the page and display the flash error message "The phone number already exists.". 当我单击sendotp按钮时,首先进行数据库验证以检查该数字是否已存在于数据库中,然后相应地它将返回0或1。如果该数字已存在,则即从控制器验证中收到1然后我要重新加载页面并显示Flash错误消息“电话号码已存在。”。 Here is a small snippet of my code. 这是我的代码的一小段。
$.ajax({
url:'{{url("ajax/checkphone")}}',
method:'post',
data:{'mobile_no':mobile,'user_id':user_id},
success:function(data)
{
obj = JSON.parse(data);
if(obj.message=='1')
{
alert("Number already exists.");
location.reload();
}
else {
//call the sendotp url and function.
}
}
});
The function is working properly as I tested it using the alert function. 当我使用警报功能对其进行测试时,该功能正常工作。
You can use \\Session::flash('message', 'Number already exists.');
您可以使用\\Session::flash('message', 'Number already exists.');
in your controller. 在您的控制器中。 Need to validate in the controller and set the flash message. 需要在控制器中进行验证并设置Flash消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.