Jump to content

Array won't add, in Javascript


njaohnt

Recommended Posts

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>

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...