test script

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
dourvas
Forum Commoner
Posts: 26
Joined: Fri Dec 05, 2008 7:21 am

test script

Post by dourvas »

hallo there

i am trying to implement a test script using php and javascript

i want a digital clock to count down time. when the time reaches to 0 it would redirect the page to another.
the number of minutes that the cock will count down it is stored into a php variable. the issue is that i am having trouble to pass my php variable to javascript the way i want.

my code

the php file

Code: Select all

$time=10;
?>
<form name="cd">
<input id="txt" readonly="true" type="text" value="<?php echo $time;?>:00" border="0" name="disp">
</form>
the javascript code

Code: Select all

var mins
var secs;


function cd() {
 	mins = 1 * m("10"); // change minutes here
 	secs = 0 + s(":01"); // change seconds here (always add an additional second to your total)
 	redo();
}

function m(obj) {
 	for(var i = 0; i < obj.length; i++) {
  		if(obj.substring(i, i + 1) == ":")
  		break;
 	}
 	return(obj.substring(0, i));
}

function s(obj) {
 	for(var i = 0; i < obj.length; i++) {
  		if(obj.substring(i, i + 1) == ":")
  		break;
 	}
 	return(obj.substring(i + 1, obj.length));
}

function dis(mins,secs) {
 	var disp;
 	if(mins <= 9) {
  		disp = " 0";
 	} else {
  		disp = " ";
 	}
 	disp += mins + ":";
 	if(secs <= 9) {
  		disp += "0" + secs;
 	} else {
  		disp += secs;
 	}
 	return(disp);
}

function redo() {
 	secs--;
 	if(secs == -1) {
  		secs = 59;
  		mins--;
 	}
 	document.cd.disp.value = dis(mins,secs); // setup additional displays here.
 	if((mins == 0) && (secs == 0)) {
  		window.alert("Ï ÷ñüíïò óáò ôåëåßùóå. Äåí ãßíåôáé íá õðïâÜëëåôå ôï äéáãþíéóìá."); 
  		window.location = "test.php" 
 	} else {
 		cd = setTimeout("redo()",1000);
 	}
}

function init() {
  cd();
}
window.onload = init;
how to i pass my php variable into the javascript (function cd)?????????
Post Reply