Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dk4210's avatar

AJAX Json response in Session

Hello Guys,

I'm trying to figure out how to get the response from JSON into my Session::

Here's my in statement in blade

  @if(Session::has('success'))
   <div class="alert alert-success">
   {{Session::get('success')}}
   </div>
   @endif

Here's the ajax request

<script>
        $( document ).ready(function() {

            $.ajaxSetup({
            headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
            });
            
           $( "#status" ).change(function() {
                var status = $('#status').prop('checked');
                if(status == true){
                    var statusValue = 1;
                }else{
                    statusValue = 0;
                }
                let _token   = $('meta[name="csrf-token"]').attr('content');
                //alert(statusValue);


                $.ajax({
                type:'POST',
                url:"{{ route('statusRequest.post') }}",
                data:{id:2, status:statusValue},
                success:function(response){
                    console.log(response);
                    if(response) {
                        $('.success').text(response.success);
                        }
                    },
               });                
                               
            }); // End Status change
        }); // End Doc ready
        </script>

Here's the controller

class AjaxController extends Controller
{
    public function updateStatus(Request $request)
    {
        $id = $request->get('id');
        $application = Application::find($id);
        $application->status = $request->get('status');
        $application->save();
        // Set up JSON respons for success message
       if($application->status == 1){
           $message = "Enabled";
       }else{
           $message = "Disabled";
       }

        return response()->json(['success'=>"Component has been $message"]);
    }
}


Finally, here is the JSON response I get back in the console

{success: "Component has been Disabled"}  or {success: "Component has been Enabled"}

Basically I'm trying to get it to flash a message when the values changes.

Thanks in Advance!

0 likes
11 replies
tykus's avatar

The PHP Session and Javasscript AJAX request/response are not going to work like you are wanting; the _first rendering based on the Session has already happened, and you are not sending back HTML, only JSON.

You are changing the content of some.success element (unseen in the code above) whenever handling the response, so why not change the visibility also - instead of the changing if the alert is render from the Session.

dk4210's avatar

Can you elaborate on "so why not change the visibility also"?

tykus's avatar

jQuery as show and hide functions to toggle the visibility of elements, use those instead of the Session check in the Blade template

dk4210's avatar

So I'm doing something like this

 if(response) {
 //$('.success').text(response.success);
  $("#success").show();
 setTimeout(function() { $("#success").hide(); }, 5000);
 }

At the top I have this

 <div id="success">Test 1234</div>

But it doesn't seem to be working. Any ideas?

tykus's avatar

Any useful information in the console?

What is your truthiness test for response doing exactly; remove that check temporarily to see if the reset of the code is working.

dk4210's avatar

Yea rest of code is working great. I get a message like this back in the console

{success: "Component has been Disabled"}

dk4210's avatar

Sorry, Actually the jquery is working as expected, meaning the div does appear and disappear. I just need to know how to pass the response to the div and that should do the trick.

tykus's avatar
tykus
Best Answer
Level 104

Ok. What have you done to hide the #success element in the first case, you need to add a display:none styling so that it is hidden by default.

Sinnbeck's avatar
$("#success").show().html(response.success);
 setTimeout(function() { $("#success").hide().html(''); }, 5000);
1 like
tykus's avatar

Missed your last reply while writing mine @dk4210 - just write the response.success to the #success elements text:

$("#success").text(response.success);

Please or to participate in this conversation.