0

I had a javascript counter. If counter = 0, then price change. I want to assing javascript price var value of php variable. But price increases once in php and it's all.

<?php global $p; $p = 1000; ?>

var pr = <?php echo $p;?>;
function downCounter(tim)
{
    var t;
    counter=tim-1;
    var elem=document.getElementById('counter');
    var price= document.getElementById('price');
    var days    = parseInt(counter / 86400),   
    hours   = parseInt(counter / 3600) % 24,   
    minutes = parseInt(counter / 60) % 60,   
    seconds = counter % 60,  
    hours   = (hours < 10) ? '0' + hours : hours;   
    minutes = (minutes < 10) ? '0' + minutes : minutes;   
    seconds = (seconds < 10) ? '0' + seconds : seconds;   
    elem.innerHTML=(days ? days + dayname : '') + hours + ':' + minutes + ':' + seconds;
    price.innerHTML=(pr);

    if (counter==0)
    {
        downCounter('43200');
        <?php $p += 500; ?> <-- works once
        pr = <?php echo $p; ?>;
    } else {
        clearTimeout(t);
        t=setTimeout("downCounter('+"+(tim-1)+"')", 1000);
    }
    return false;
}
downCounter('43200');
</script>

thanks to all, but how i can protect my pr variable from changing in source and then send via ajax?

  • 1
    php is serverside. javascript is clientside. Your server parses php code and outputs it (including the javascript) to the client (your browser). Your browser knows nothing about php. Righclick and view source. You will see that your php code isn't even there. If you want php and javascript to work together like that, then you're going to have to do some restructuring and make use of AJAX. – CrayonViolent Jun 06 '14 at 15:01
  • I assume you have a ` – RiggsFolly Jun 06 '14 at 15:05

1 Answers1

1

The problem here stems from your confusion about context: PHP runs on the server and outputs HTML which is sent to the client; Javascript runs on the client and does not communicate with the server (unless you use AJAX or other methods).

The PHP is executed on the server and sent to the client, so the client gets something like this:

var pr = 1000; // inserted with the PHP echo statement
function downCounter(tim)
{
    var t;
    counter=tim-1;
    var elem=document.getElementById('counter');
    var price= document.getElementById('price');
    var days    = parseInt(counter / 86400),   
    hours   = parseInt(counter / 3600) % 24,   
    minutes = parseInt(counter / 60) % 60,   
    seconds = counter % 60,  
    hours   = (hours < 10) ? '0' + hours : hours;   
    minutes = (minutes < 10) ? '0' + minutes : minutes;   
    seconds = (seconds < 10) ? '0' + seconds : seconds;   
    elem.innerHTML=(days ? days + dayname : '') + hours + ':' + minutes + ':' + seconds;
    price.innerHTML=(pr);

    if (counter==0)
    {
        downCounter('43200');
        pr = 1500; // $p += 500 on previous line of PHP code; echoed here
    } else
    {
        clearTimeout(t);
        t=setTimeout("downCounter('+"+(tim-1)+"')", 1000);
    }
    return false;
}
downCounter('43200');
</script>

That's what's being executed on the client - see why it's not working the way you want? Javascript is working with static values: 1000 and 1500.

As mentioned above, if you want those values returned to the server, you'll need to write some Javascript code to actually send the values back.

Kryten
  • 15,230
  • 6
  • 45
  • 68