njaohnt Posted September 15, 2011 Posted September 15, 2011 I have my Javascript, which is outputting 'NaN'. I don't want it to, and I want it to display the difference between their computer, and the server. <html><head><title>Time</title> <script type="text/javascript">function d() {var j=[<?php echo idate("H");?>,<?php echo idate("i");?>,<?php echo idate("s");?>]; var t=new Date(); var c=[t.getHours,t.getMinutes,t.getSeconds]; var a=[c[0]-j[0],c[1]-j[1],c[2]-j[2]]; var b="After Ours"; if (a[0]<0) {a[0]=j[0]-c[0]; b="Before Ours";} if (a[1]<0) a[1]=j[1]-c[1]; if (a[2]<0) a[2]=j[2]-c[2]; for (i=0;i<3;i++) document.getElementById("r"+i).innerHTML=a[0]; document.getElementById("r").innerHTML=b; document.getElementById("t").innerHTML="Your computer's date and time:"+Date()+"<br>Our date and time:<?php $aop="AM"; if (idate('H')>12) $aop="PM"; $dow=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); $mow[1]='January'; $mow[2]='February'; $mow[3]='March'; $mow[4]='April'; $mow[5]='May'; $mow[6]='June'; $mow[7]='July'; $mow[8]='August'; $mow[9]='September'; $mow[10]='October'; $mow[11]='November'; $mow[12]='December'; $min=idate("i"); $sec=idate("s"); if ($min<10) $min="0".idate("i"); if ($sec<10) $sec="0".idate("s"); $d=array($dow[idate('w')],idate('d'),$mow[idate('m')],idate('Y'),idate('h'),$min,$sec,microtime(get_as_float),$aop); echo $d[0]." ".$d[1]." ".$d[2]." ".$d[3]." ".$d[4].":".$d[5].":".$d[6].".".$d[7]." ".$d[8]; ?>";}</script> </head><body onload="d()"><div id="t"></div><br><div id="r0"></div>h <div id="r1"></div>m <div id="r2"></div>s <div id="r"></div> <button onclick="window.location.href='servertime.php'">Update Time</button> </body></html>
PenTester Posted September 16, 2011 Posted September 16, 2011 For me it showing(when i use xampp) h m s Update Time[button]
njaohnt Posted September 16, 2011 Author Posted September 16, 2011 So how would I get the numbers on there instead on NaN. Why isn't it a number?
PenTester Posted September 17, 2011 Posted September 17, 2011 Actually what do you want to do? and why do you want to do?
njaohnt Posted September 17, 2011 Author Posted September 17, 2011 I want it to have the difference in time on there so that they can submit a form on to the proper time.
Tjoene Posted September 18, 2011 Posted September 18, 2011 So how would I get the numbers on there instead on NaN. Why isn't it a number? JavaScript has unlike Java no type assigned to a VAR. The type can be changed when you assign a value. To make sure you get a number and not a string you could force typecast the VAR to an integer (parseInt).
Byron Posted September 18, 2011 Posted September 18, 2011 I'm like xaav, "can you post an example"? Where did you get this script? I'm thinking (and I could be wrong) the problem is that your trying to execute php through the browser when it's a server side language. Javascript is browser side. Your php is working correctly when it's ONLY php on a page: http://byrondallas.heliohost.org/temp/java-scrip-php.php
njaohnt Posted September 18, 2011 Author Posted September 18, 2011 Yes, it is on a php page. How do I change it's type Tjoene?
Byron Posted September 18, 2011 Posted September 18, 2011 What I was trying to say is that your trying to execute the php through the javascript. You can't execute php through the client. Or either your javascript is wrong. I've never really learned javascript so I can't help you there.
Wizard Posted September 18, 2011 Posted September 18, 2011 {var j=[<?php echo idate("H");?>,<?php echo idate("i");?>,<?php echo idate("s");?>]; Does this return an array of strings or an array of numbers? Also, rewrite your code to follow good practices: http://javascript.crockford.com/style1.html
njaohnt Posted September 18, 2011 Author Posted September 18, 2011 Numbers. The source of the page has unquoted numbers.
Byron Posted September 19, 2011 Posted September 19, 2011 I still say that your problem is your trying to execute php through a javascript function and it won't work. Quoted from the this thread: http://www.codingforums.com/archive/index.php/t-2600.html To understand why your approach is not working, it's really important to remember that JavaScript runs on the client and PHP on the server. As Spookster has already said above, when PHP is done with processing a file, it sends the output as a HTTP response back to the client. So generally, these two technologies can't be interoperable, since they don't run in the same environment.
Guest xaav Posted September 19, 2011 Posted September 19, 2011 Will you please post a link to your page so I can figure out what you want?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now