0

I'm trying to make a little javascript loader. My code:

<?
//COUNT TOTAL PURCHASE INVOICES
$i = 0;
foreach($xml->tr as $purchase) 
{
    $i++;
}
$steps = 100 / $i;

$steps = str_replace(',', '.', $steps);


foreach($xml->tr as $purchase) 
{   
    ?>
    <script type="text/javascript" charset="utf-8">
    $(document).ready(function() 
    {
        var loaderValue = $("#loaderValue").val();
        newLoaderValue = parseFloat(loaderValue) + parseFloat("<?=$steps?>");

        $("#loaderValue").val(newLoaderValue);
        $(".connect_loader").width(newLoaderValue+"%");
    });
    </script>
    <?
}
?>

The script worked but the problem is that the javascript runs after php is ready so the loader is in one step at 100%.

Is there another simple way to do this?

1
  • it is required to use '$(document).ready(function() ..'? Commented Sep 5, 2013 at 7:29

3 Answers 3

2

Your question is a bit confusing.

You have to know that the PHP is used server side and the javascript client side.

The script worked but the problem is that the javascript runs after php is ready so the loader is in one step at 100%.

So this behavior is normal.

The best way to do what you want to do is to make a Javascript function which do an Ajax call and increment your loader depends on the result of the function.

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

2 Comments

Yes that's what I found out. That's why I asked for another solution.
So when I use an Ajax call (in the foreach) to a page with the loader function that will work?
0

PHP is not visible to the client. The server will process the PHP part of your code and generate a html page which the client will see.
This means you cannot use PHP during runtime.
Try to store whatever values you need into Javascript variables and use those instead.
A simple way to understand this is to view page source on your webpage... You can see the resultant html code.

Comments

-1

I hope this works.

<?php
//COUNT TOTAL PURCHASE INVOICES
$i = 0;
foreach($xml->tr as $purchase) 
{
    $i++;
}
$steps = 100 / $i;

$steps = str_replace(',', '.', $steps);
?>
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() 
    {
        for(var x = 0; x <= <?php echo $i ?>; x++){
            var loaderValue = $("#loaderValue").val();
            newLoaderValue = parseFloat(loaderValue) + parseFloat("<?php echo $steps ?>");

            $("#loaderValue").val(newLoaderValue);
            $(".connect_loader").width(newLoaderValue + "%");
        }
    });
</script>

Comments

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.