Using PHP in a Javascript function?
Posted: Sat Aug 07, 2010 1:29 pm
I'm assuming this is a pretty basic question, but is the code that I posted going to work properly? I just inerted a mysql query into a javasdcript function. The function is called by an onClick event handler.
Edit:
As for the context, this is supposed to be a countdown and I want to insert the current timestamp into my database when the function is called.
My problem is that the timestamp is inserted into the database whether the function is called or not. So, the php part runs as soon as the page is loaded. How can I change this?
Code: Select all
function countdown(link2) {
$(link2).removeAttr('onclick');
time--;
<?php
$time = time();
$insert_query2 = "INSERT INTO test_user_timestamp (username,building,timestamp_built) VALUES ('Patrick','Kristallmiene',$time)";
mysql_query($insert_query2) or die("Error, insert query failed");
?>
if (time <= 0) {
text = 'Countdown finished';
}
else {
hour=Math.floor(time/(60*60));
rest=time%(60*60);
min=Math.floor(rest/60);
rest=rest%60;
text = hour + ":" + min + ":" + rest;
window.setTimeout('countdown()', 1000);
}
document.getElementById('time2_div').firstChild.data = text;
}
As for the context, this is supposed to be a countdown and I want to insert the current timestamp into my database when the function is called.
My problem is that the timestamp is inserted into the database whether the function is called or not. So, the php part runs as soon as the page is loaded. How can I change this?