Results 1 to 5 of 5

Thread: php/javascript updating script

  1. #1
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default php/javascript updating script

    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>

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •