PDA

View Full Version : php/javascript updating script



neo834
11-07-2006, 08:07 AM
I have a problem with this script it doesnt work :(
im trying to do a script that updates itself every second took this from a
clockscript and modified it but somethings wrong at the "visar="
anyone knows how to fix it?

<head>
<BODY>
<SPAN ID="text" style="position: absolute;"> </SPAN>

<SCRIPT LANGUAGE="JavaScript">

lagerNamntext="text";

function uppdatering()
{
visar="
<?php
$open = mysql_connect('XXXXXX', 'XXXXXX', 'XXXXX');
$result= mysql_db_query('XXXXX', 'SELECT * FROM textdatabas where id = '1' ');
row=mysql_fetch_array($result);
echo $row['text'];?>
"

if (document.all)
{
document.all[lagerNamntext].innerHTML = visar;
}
else if (document.layers)
{
with (document[lagerNamntext].document)
{
open();
write(visar);
close();
}
}
else if (document.getElementById)
{
document.getElementById(lagerNamntext).innerHTML=visar; //Netscape 6 har denna hierarki
}

setTimeout("uppdatering()", 1000);
}
setTimeout("uppdatering()", 100);
//-->
</SCRIPT>

djr33
11-07-2006, 08:19 AM
PHP doesn't run in realtime.

It is run ONCE by the server, then it outputs html, javascript, etc. that make up the source code.

Your source is then looping through the same output from php each time.

AJAX is what you need.

But a 1 second refresh rate is awfully fast and could overload things.

neo834
11-07-2006, 08:22 AM
ok? i have never used AJAX..
but doesnt this work at all?
if i run the script again doesnt i check the datebase and writing out the new value?

djr33
11-07-2006, 08:25 AM
No.

Javascript doesn't run PHP.

PHP is run ONCE, then it sends SOURCE CODE to the browser.

ANYTHING in <?php .... ?> tags is replaced with text-only output from the PHP WHEN IT'S RUN.

Then, from that source, html, javascript, css, and a couple other things, are run.

Javascript, then, it constantly looping through the value of, eg, 3, if that was the output of the php script. It won't ever change.


AJAX uses a httprequest function that basically refreshes with javascript, not the user's browser, so it grabs the page off the server, thereby running it through the server.

It's complex.

There's a script on DD... "simple ajax routine", I think. Look for that.
There are also tutorials about it. Google.

By the way, AJAX is JUST a name for a style of using javascript, not a different language.

neo834
11-07-2006, 08:29 AM
oh... ok... i thought there was a easy way to do this... but apperantly not :(
hmm i think that i have used AJAX before in a search thing when i wrote the name it popped out directly.. hmm i can maybe use that intressting.. thanks for the tip :)