Below is something simple i created where everytime a page reloads a box will either be black or white. what i want to create is for this to automaticly happen without a refresh. for it to go black-white-black-white etc. reading up on php its telling me that the code is executed before the page loads. so i dont think its possible. Is it possible to do what im trying to accomplish? thanks <? $black="bgcolor='black'"; $white="bgcolor='white'"; $name=1; $name2=2; $random=rand($name,$name2); if($random==1) { Print" <TABLE BORDER=1> <TR> <TD $black width='25' height='25' value='value' name='name'></TD> </TR> </TABLE>"; }else{ Print" <TABLE BORDER=1> <TR> <TD $white width='25' height='25' value='value' name='name'></TD> </TABLE>"; } ?> PHP:
Y your are doing it with PHP then? Create a random number in JS, and use function setTimeOut, Following is hint <script> function changeColor() { var num = sometechniqueheretogeneraterandomnumber; if(num == 1) var color = "ffffff"; else var color = "000000"; document.getElementById("mytd").style.background = color; } setTimeout("changeColor()", 5000); </script> <TABLE BORDER=1> <TR> <TD id="mytd" width='25' height='25' value='value' name='name'></TD> </TR> </TABLE> Code (markup): I have not tested it, but it shud work.
Here is a more advanced, yet easy to use random string generator: function randChar($x, $r) { $rand =''; for ($a = 0; $a < $x; $a++) { $b = rand(0, strlen($r) - 1); $rand .= $r[$b]; } return $rand; } PHP: The following allows specifying which characters should be used to generate the string echo randChar(5, '1234567890'); PHP: "5" represents the number of chars that the string should contain. "1234567890" is the character selection for the string to be generated from.