Forum Moderators: open
This is my code now :
<html>
<head>
</head>
<body bgcolor="#000000" topmargin="10">
<center>
<span id="digitalclock" class="styling">
<script language="JavaScript">
<!--
var standardbrowser=!document.all&&!document.getElementById
var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var montharray=new Array("December","January","February","March","April","May","June","July","August","September","October","November")
if (standardbrowser)
document.write('<form name="tick"><input type="hidden" name="vip" value="4d6905b7b299d396eb8c" /><input type="text" name="tock" size="11"></form>')
function show(){
if (!standardbrowser)
var clockobj=document.getElementById? document.getElementById("digitalclock") : document.all.digitalclock
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
month+=1
var daym=mydate.getDate()
if (daym<10) daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn=""
if (hours>=12) dn=""
if (hours>24){
hours=hours-12
}
if (hours==0) hours=00
if (minutes<=9) minutes="0"+minutes
if (seconds<=9) seconds="0"+seconds
if (standardbrowser){
document.tick.tock.value=dayarray[day]+", "+yeararray[month]+". "+month+". "+daym+". "+hours+":"+minutes+":"+seconds
} else{
clockobj.innerHTML="<font color='#FFFF00' size='2' face='Verdana'>"+year+"<BR>"+montharray[month]+" "+daym+".<BR> "+dayarray[day]+"<BR>"+hours+":"+minutes+":"+seconds+"<BR></sup></font>"
}
setTimeout("show()",1000)
}
window.onload=show
//-->
</script>
</span>
<BR><BR>
<font color="yellow" size='2' face='Verdana'>
<?php
$today = getdate();
$mon = $today['mon'];
$day = $today['mday'];
$this_day = ("$mon"."$day");
$connection = mysql_connect("HOST", "USERNAME", "PASSWORD") or die("I can't join");
mysql_select_db("DATABASENAME") or die("Cannot choose database");
$search = "SELECT names FROM nameday WHERE (nameday.days='$this_day')";
$result = mysql_query($search) or die("Error in search");
$result_array = mysql_fetch_array($result);
echo $result_array["names"];
mysql_free_result($result);
mysql_close($connection);
?>
<BR>
</body>
</html>
the only way they can communicate is when the page is requested.
javascript is client side
php is server side
if javascript wants to send data to php it would have to be appended to the url or posted to the server somehow.
php could dynamically write the actual javascript code into the page.
aside from that they can't really communicate
you would dynamically write the PHP variable the variable into the Javascript ... just like you would use PHP to write a value to the HTML ... either way, you're just writing a value to the outputstream.
I don't really know PHP but the concept is the same as JSP/AS and others. I'll show you an example in JSP. The first line is the jspvariable being defined. The second line is the jsp variable being written into the javascript:
<%String jspvar = "hello";%>
var jsVariable = "<%=phpVar%>";
In the case of your specific example...and using PHP, I'm guessing it would look something lke this:
clockobj.innerHTML=clockobj.innerHTML+<?=$result_array["names"]?>
...note I just put the context switch wrapper around the variable that tells the php processor to grab and write out the PHP variable you're referencing.
Good luck.
This type of thing can go anywhere in a script, assigning PHP values to JS variables:
yourJSvariable="<?=$PHPthingy?>";
In fact, PHP injections can go anywhere in a PHP-parsed page, as one-liners or as big chunks. I use it to output dynamic HTML all the time.
<b>Today is: <?=date("F j, Y")?></b><br />
or the ever-handy:
Copyright ©<?=date("Y")?> by MyClient
Lots more examples (tomorrow, three days from now, etc.) in the manual (see date() and mktime()).
Thanks for alerting me to the echo shortcut, cabbagehead! :)
Or is it that you want to open a the JS page and see the change according to what time it is?
The PHP date() functions get their data from the server, including the base language. If the server is set to GMT-5 and your system is set to GMT-4 ... there's some display:systemtime sync offset.
JS gets it's time info from the client system, so the same discrepancy would be visible if you mixed the two in your functions. You could adjust one or the other to compensate. Details for PHP are in the manual.
You currently have a JS "ticker" kind of thing that changes the time display every second and, when it hits midnight, changes the greeting based on a name you are pulling from a db with PHP.
You want the JS ticker variable to trigger the PHP activity when it hits midnight, pulling the name from the db into the JS greeting.
If so ... and I could need correction ...
PHP is server-side, and needs to run on the server. There's no PHP plugin (yet) that lets a client environment variable modify a PHP variable. Since the PHP is all gone by the time the page gets to the browser, there's nothing there to interact with the client-side stuff, like JS.
You will need to make a call to the server and refresh some part of the page from there. Some JS src attribute that is not cached, maybe. It needs to be a mini file reload, or a whole-page reload ... which I know is unacceptable.
You might use the PHP to get a wider result set from the database when the page is first requested. Instead of just pulling the one name that is appropriate only for the moment the page was compiled, you could pull, say 7 days' worth of names into a JS array, and use those entries for your script. Then set JS to refresh the whole page once per week.
Just thinking out loud ... :)