Epiales wrote:Jackolantern wrote:I guess another piece of Javascript not shown would change it? Because he is right that the code you showed us would never change since the PHP runs only once on the server side.
I was thinking that it could do a ECHO the IMAGE of the progress bar and wait 5 seconds and then ECHO the results of the attack? There should be a way to do this with setInterval or something???? Maybe? lol
No, you can't do that. Remember, even though you can mix PHP in the Javascript code, they work in totally different ways and at totally different times. The PHP runs only
once, when the initial request comes into the server. The PHP runs, and writes the HTML/CSS/Javascript response to send to the server. For example:
Code: Select all
document.getElementById("lose_bullets").innerHTML = "<?php echo $image;
This code will add the value of the $image variable into this line of text while the PHP is executing on the server (not while the Javascript is executing). Say the $image variable holds "progress.png". Once the PHP is done executing, this is what is sent to the browser:
Code: Select all
document.getElementById("lose_bullets").innerHTML = "progress.png";
Once the code reaches the browser,
then the Javascript is executed, and "progress.png" is hard-coded into the Javascript, meaning you would be calling a static piece of Javascript over and over with the interval. It won't change because the PHP has already been executed.
PHP and Javascript don't interact like that. The only interaction they can have is that PHP can be used to write Javascript code, but once that Javascript reaches the browser, it is set in stone. The PHP is done executing and won't change again.
Anything that requires time, or needs to be checked again against user actions must be done 100% in Javascript. Or it could be done with PHP through further page loads. But just remember that PHP only runs on the server once to generate the HTML/CSS/Javascript response to the browser.
EDIT: After looking a bit more at what you are wanting to do (click a button to attack other players), it looks like you will need to use AJAX for that. The easiest way to implement AJAX is to use jQuery, so I would suggest checking that out. AJAX is the only way that the Javascript executing on the page can call back to PHP scripts on the server.