admin管理员组

文章数量:1022695

Here is my scenario:

-I'm processing an excel file that'll took about ~10 minute
-While processing, I want to send a feedback to user that we are currently processing

My idea was using Session that will put value while processing, and get that Session using javascript in current view
Here's my script:

<script type="text/javascript">
    $(document).ready(function() {
        var element = document.getElementById("progress");
        setInterval(
            function(){
                element.innerHTML = "{{Session::get('progress')}}";
            },500
        );
    });
</script>


And somewhere in my controller, let's just say like this:

    $i = 0;
    while(!$done){
        processingComplicated();
        Session::put('progress', $i);
        Session::save();
    }


And my basic view :

<div id="progress">0</div>

Basically, I want the current page to get the Session data and update the view(id="progress"), but the it won't change.
Can it be done? Thanks.

Here is my scenario:

-I'm processing an excel file that'll took about ~10 minute
-While processing, I want to send a feedback to user that we are currently processing

My idea was using Session that will put value while processing, and get that Session using javascript in current view
Here's my script:

<script type="text/javascript">
    $(document).ready(function() {
        var element = document.getElementById("progress");
        setInterval(
            function(){
                element.innerHTML = "{{Session::get('progress')}}";
            },500
        );
    });
</script>


And somewhere in my controller, let's just say like this:

    $i = 0;
    while(!$done){
        processingComplicated();
        Session::put('progress', $i);
        Session::save();
    }


And my basic view :

<div id="progress">0</div>

Basically, I want the current page to get the Session data and update the view(id="progress"), but the it won't change.
Can it be done? Thanks.

Share Improve this question edited Sep 14, 2017 at 4:43 Irfandi D. Vendy asked May 11, 2015 at 9:25 Irfandi D. VendyIrfandi D. Vendy 1,0041 gold badge14 silver badges20 bronze badges 1
  • It can't be done the way you want - you'll need to have the page reload in order to actually get fresh session data. A lot of people do this using an iframe or using AJAX - the main point is that you need to actually load the PHP file to get the updated value, you can't just put the session var in your page somewhere and expect it to magically update without updating the page. – alexrussell Commented May 11, 2015 at 9:39
Add a ment  | 

1 Answer 1

Reset to default 2

Use AJAX to update the view

$(document).ready(function() {
    var element = document.getElementById("progress");
    setInterval(
        function(){
            $.get( "processing-status", function( data ) {
                 element.innerHTML = data;
            });
        },500
    );
});

And in your routes, controller or wherever you want

Route::get('/processing-status', function()
{
    return Session::get('progress');
});

Here is my scenario:

-I'm processing an excel file that'll took about ~10 minute
-While processing, I want to send a feedback to user that we are currently processing

My idea was using Session that will put value while processing, and get that Session using javascript in current view
Here's my script:

<script type="text/javascript">
    $(document).ready(function() {
        var element = document.getElementById("progress");
        setInterval(
            function(){
                element.innerHTML = "{{Session::get('progress')}}";
            },500
        );
    });
</script>


And somewhere in my controller, let's just say like this:

    $i = 0;
    while(!$done){
        processingComplicated();
        Session::put('progress', $i);
        Session::save();
    }


And my basic view :

<div id="progress">0</div>

Basically, I want the current page to get the Session data and update the view(id="progress"), but the it won't change.
Can it be done? Thanks.

Here is my scenario:

-I'm processing an excel file that'll took about ~10 minute
-While processing, I want to send a feedback to user that we are currently processing

My idea was using Session that will put value while processing, and get that Session using javascript in current view
Here's my script:

<script type="text/javascript">
    $(document).ready(function() {
        var element = document.getElementById("progress");
        setInterval(
            function(){
                element.innerHTML = "{{Session::get('progress')}}";
            },500
        );
    });
</script>


And somewhere in my controller, let's just say like this:

    $i = 0;
    while(!$done){
        processingComplicated();
        Session::put('progress', $i);
        Session::save();
    }


And my basic view :

<div id="progress">0</div>

Basically, I want the current page to get the Session data and update the view(id="progress"), but the it won't change.
Can it be done? Thanks.

Share Improve this question edited Sep 14, 2017 at 4:43 Irfandi D. Vendy asked May 11, 2015 at 9:25 Irfandi D. VendyIrfandi D. Vendy 1,0041 gold badge14 silver badges20 bronze badges 1
  • It can't be done the way you want - you'll need to have the page reload in order to actually get fresh session data. A lot of people do this using an iframe or using AJAX - the main point is that you need to actually load the PHP file to get the updated value, you can't just put the session var in your page somewhere and expect it to magically update without updating the page. – alexrussell Commented May 11, 2015 at 9:39
Add a ment  | 

1 Answer 1

Reset to default 2

Use AJAX to update the view

$(document).ready(function() {
    var element = document.getElementById("progress");
    setInterval(
        function(){
            $.get( "processing-status", function( data ) {
                 element.innerHTML = data;
            });
        },500
    );
});

And in your routes, controller or wherever you want

Route::get('/processing-status', function()
{
    return Session::get('progress');
});

本文标签: phpLaravel get Session with javascript while loadingStack Overflow