1

I try to make progress bar, the css code like this:

#result{
    background:#8c8f91;
margin-left: 15px;
margin-right: auto;
table-layout: fixed;
border-collapse: collapse;
z-index: -1; position:relative;
width: 0%;
}

And I want to update my progress bar with Ajax:

       $(function worker(){
    // don't cache ajax or content won't be fresh
    $.ajaxSetup ({
        cache: false,
        complete: function() {
          // Schedule the next request when the current one's complete
          setTimeout(worker, 4500);
        }});
   $("#result").css("width"," <?php echo json_encode($percent); ?>");
   $("#result").load("http://localhost/test6/select-oki.php #result").fadeIn();
}
// end  
});

I want to update "width" value with "$percent", but it didnt get value from it. I think the problem is in this :

$("#result").css("width"," <?php echo json_encode($percent); ?>");
2
  • try <? php echo $percent ?> Commented Nov 25, 2017 at 3:54
  • I already try it but still not working :') Commented Nov 25, 2017 at 5:22

1 Answer 1

1

A few things about the original code. $percent is not defined in js, at least the code you are showing. You are requesting json_encode before even attempting a load. I have not tested this, just some stuff I noticed.

Consider this approach

$(function worker(){
    $.ajax({
        cache: false,
        url:'http://localhost/test6/select-oki.php',
        success: function(data) {
            var response = JSON.parse(data);
            $("#result").css("width",response['percent']).fadeIn();
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log('error!');
        }
    });
});

Echo your data out from PHP echo json_encode(['percent'=>$percent]);

Sign up to request clarification or add additional context in comments.

2 Comments

I made a fiddle with the same idea, but changed the .css('width', pct) to .animate('width', pct). Here is the link jsfiddle.net/p3vfdu2z/2
Make sure you return your url and remove the data properties, I modifed those to show you a working example. Once you return your url, and this does not work, you likely have an error in the php file you are accessing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.