1

I have created ajax function when the user clicks the button, it will check first the users profile if account is already confirmed. If not, it will redirect back to user dashboard. However, my problem now is that the page is not displayed or it is not redirecting back. The result can only be seen in the browsers' network tab.

my ajax

$(document).on("click", "#apply", function(){
$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

$.ajax({
  type: "get",
  url: '/checkstatus',
  success: function(store){
    if(store == 'confirmed'){
      $(".apply_modal").toggleClass("open").show();
      $("body").toggleClass("open");
    }
  },

});

});

and my controller:

public function checkStatus(Request $request)
{
    $verify = Auth::user()->verifyAccount();

    if($verify == false){
        if(session()->has('verify') && session()->get('verify') != '') {
           session()->forget('verify');
        } else {
            session()->flash('verify', 'At first, please update your profile!');
        }
    }else{
        return 'confirmed';
    }

}

How can I properly redirect back the user to its main page? The result for now is like this.

enter image description here

Message to the user :

@if(session('verify'))
    <div class="complete_box">
        <p>{{ session('verify') }}</p>
        <a href="{{ url('/mypage') }}">Close</a>
    </div>
    @endif
Eem Jee
  • 1,239
  • 5
  • 30
  • 64

2 Answers2

3

you can not redirect through controller if you are using ajax. you need to use javascript

window.location.href = "your url";
window.location.href = "{{url('/mypage')}}";
$(document).on("click", "#apply", function(){
$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

$.ajax({
  type: "get",
  url: '/checkstatus',
  success: function(store){
    if(store == 'confirmed'){
      $(".apply_modal").toggleClass("open").show();
      $("body").toggleClass("open");
      window.location.href = "your url";
    }
  },

});

});

for flash message use session

public function checkStatus(Request $request)
{
    $verify = Auth::user()->verifyAccount();

    if($verify == false){
        \Session::put('message','At first, please update your profile!');
        return 'something you want';
    }else{
        return 'confirmed';
    }

} 

Now in blade file where you want to show flash put this

@if(Session::has('message'))
    <p class="alert alert-success">
       {!! Session::get('message') !!}
       <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
    </p>
@endif  
Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
  • Thanks for this. It works, however, it is missing with the message `['verify' => 'At first, please update your profile!']`. How can I do it that way? – Eem Jee Mar 13 '19 at 11:57
  • @EemJee Take a look at https://stackoverflow.com/questions/282429/returning-redirect-as-response-to-xhr-request/2573589#2573589 effectively your message will be set and then consumed within the same request. You need to revise your overall logic – apokryfos Mar 13 '19 at 11:59
  • @AkashKumarVerma Yes. as a prompt to the user that they have to update the profile. – Eem Jee Mar 13 '19 at 12:03
  • @EemJee please check and verify it if you facing any issue let me know – Akash Kumar Verma Mar 13 '19 at 12:12
  • Thanks @AkashKumarVerma It works. But please check my updates in sending the user a message, the my url when the close button is clicked is `/mypage` . My problem now, is that the modal is not closing. – Eem Jee Mar 13 '19 at 12:15
  • are you sing bootstap model – Akash Kumar Verma Mar 13 '19 at 12:18
  • @AkashKumarVerma Yes. I'm trying to use flash instead of put. But still the same. – Eem Jee Mar 13 '19 at 12:19
  • @AkashKumarVerma Please check. – Eem Jee Mar 13 '19 at 12:23
  • you made a mistake if(ession()->has('verify') && $request->session()->get('verify') != '') { =========== s is missing in session – Akash Kumar Verma Mar 13 '19 at 12:26
  • and in flash you need to use $request->session()->flash('verify', 'At first, please update your profile!'); – Akash Kumar Verma Mar 13 '19 at 12:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189948/discussion-between-eem-jee-and-akash-kumar-verma). – Eem Jee Mar 13 '19 at 12:29
1
$(document).on("click", "#apply", function(){
$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

$.ajax({
  type: "get",
  url: '/checkstatus',
  success: function(store){
    if(store == 'confirmed'){
      $(".apply_modal").toggleClass("open").show();
      $("body").toggleClass("open");
      window.location.href = "store.url";
    }
  },

});

});

Here the store.url is that url which is coming in response.

In controller

Craete a url and send in response.