重新加载laravel视图后,如何使用ajax显示flash消息

问题描述:

I have a form in my laravel view. 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 i.e. 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.". 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.

我的laravel视图中有一个表单。 当我点击sendotp按钮时,首先进行数据库验证以检查数据库中是否已存在该数字,然后相应地它将返回0或1.如果该数字已经存在,那么我从控制器验证中收到1 然后我想重新加载页面并显示闪存错误消息“电话号码已存在。”。 这是我的代码的一小部分。 p>

  $ .ajax({
 url:'{{url(“ajax / checkphone”)}}',
方法 :'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 {
 //调用sendotp url和函数。
  n} 
} 
}); 
  code>  pre> 
 
 

当我使用警报功能测试它时,该功能正常工作。 p> div >

You can use \Session::flash('message', 'Number already exists.'); in your controller. Need to validate in the controller and set the flash message.