/    Sign up×
Community /Pin to ProfileBookmark

PHP within a javascript loop… how to get it to execute each time?

It seems like my PHP is only executing the first time through the loop… anyone know what is wrong here?

<script type=”text/javascript”>
var x;
var curtime;
x=0;
while(x<5)
{

<?php
sleep(5);
$curtimedisplay = date(“h:i:s”);
$jobcur = strtotime($curtimedisplay);

$query = “UPDATE jobinstances SET jobstop=’$jobcur’ where id = ‘$id'”;
$result = mysql_query($query)
or die(mysql_error());

?>

curtime = “<?php echo $curtimedisplay; ?>”;
document.getElementById(‘curtime’).innerHTML=curtime;
alert(curtime);
x=x+1;

}
</script>

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 02.2007 — PHP executes on the [i]server[/i].

JavaScript executes on the [i]client[/i] (browser).

Therefore, by the time your JavaScript actually starts executing, PHP has done its job. What your JavaScript executes in each loop is whatever the [i]output[/i] was of the PHP code when it executed on the server, so in this case it is always that same output on each JS loop iteration.
Copy linkTweet thisAlerts:
@xvszeroauthorNov 02.2007 — That sucks.

Hmm. Now what to do. Considering I am just using it to track time I guess I could have the page refresh every minute or so... and then the PHP would work every time the page refreshes, no?

Otherwise I'm not sure how to do this.
Copy linkTweet thisAlerts:
@bejitto101Nov 02.2007 — Well theres this function in Javascript called setinterval where you can have a section of code run every so often. Might help ya out with whatever youre doing. You can find some more info here: http://www.w3schools.com/htmldom/met_win_setinterval.asp. If you sent your js to run a php through ajax, you may be able to update your db that way.
Copy linkTweet thisAlerts:
@NogDogNov 02.2007 — You could implement an AJAX solution, having your JavaScript loop call a separate PHP script on the server to do the database update and sleep().
Copy linkTweet thisAlerts:
@bokehNov 02.2007 — Why make this so complicated? It reminds me of a story from the space race. The USA spent over one million dollars (in the 60's) developing a pen that would write in a zero gravity environment. The Russians on the other hand took a pencil with them on their missions.

Probably the easiest way would be an iframe calling a page with a header refresh. No need for javascript or anything exotic.
×

Success!

Help @xvszero spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...