Forum Moderators: open

Message Too Old, No Replies

Can Javascript use a PHP variable?

         

Xeung

9:34 pm on Sep 17, 2004 (gmt 0)

10+ Year Member



I need to refresh a data every day at 00:00. In my country we're used to celebrate name-day for example on 11.14 we greet everyone called Alice. Now, my problem is that I have a mySQL database with the names. I read from it properly without any complications and I use Javascript for the clock.
So I want to know how can I do to change a name of the day automatically when the day has changed.
I suppose I need to find out how can Javascript and PHP communicate with aech other, but I'm not sure.
Please help me, if you can!

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>

jatar_k

11:42 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Xeung,

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

Xeung

7:37 am on Sep 18, 2004 (gmt 0)

10+ Year Member



Yes, but my problem is that I want to reach a PHP variable from Javascript, I want to do this

clockobj.innerHTML=clockobj.innerHTML+$result_array["names"]

So I need some methods how can I give variable from PHP to Javascript

cabbagehead

4:30 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As Jatar indicated,

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.

Xeung

5:18 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



Cabbagehead!

Thank you very much! :)
It works with the following method :

Clockobj.innerHTML=clockobj.innerHTML+"<BR><? echo $x?>"

StupidScript

5:37 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once you get a look at the options for formatting a date with PHP, all will be clear to you!
Here's the relevant section in the manual [php.net].

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 &copy;<?=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! :)

Xeung

5:55 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



Now there is on another problem it doesn't refresh the name at midnight what can I do now? How can I call a PHP function in JS on everyday at 00:00?
Or someone knows any possible methods for this "refresh"?

Xeung

5:59 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



Stupidscript, but is there possible to write this "watch" in PHP? Because it's refreshing in every second, like a watch...that's why I use JS..

StupidScript

6:01 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is your goal to have a page with the JS open all the time, and then have some of the page display change at midnight?

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.

StupidScript

6:28 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I see what you're trying to do ...

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