Help: Simple auto refresh with ajax/javascript...

Discussion in 'JavaScript' started by Skillman13, Nov 17, 2009.

  1. #1
    I've recently wanting to learn code, so I started learning php, and now I want to start ajax/javascript (For its refreshing abilities...)

    I've tried the following code in my site, I don't have a clue how it works (Well it doesnt work! :() I don't have a clue on how its supposed to work! :)

    Could anyone either fix this code or suggest code that could do what I want...
    /Or explain the fixed code. :)

    <script type="text/javascript" src="js/mootools.js"></script>

    <script language="javascript">
    var request = new Request({
    url: '/live.php',
    method: 'get',
    update: 'refresh-me',
    onComplete: function(response) {
    $('/live').set('php',response);
    }
    })

    var doRefresh = function() {
    request.send();
    };

    doRefresh.periodical(5000);
    </script>
    <div id='divid'></div>


    I don't have a clue whats right and whats wrong, please help...

    -What I want, I have a page... index.php, i want it to display a box...
    (<?php include('live.php'); ?>) which has stuff in...

    I want live.php to refresh -in the index.php box, without refreshing the whole page (the index page)

    If the code is right, i must have misplaced it in a wrong folder or whatever....

    Can anyone share a working code/example,

    -Using my file names.
    -And yes i uploaded mootools.js in a folder /js/ -I don't really need this if u have a better solution :)

    Thanks alot,

    James
     
    Skillman13, Nov 17, 2009 IP
  2. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Any advice at all?
     
    Skillman13, Nov 17, 2009 IP
  3. mps705

    mps705 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It looks like Prototype Ajax. Have you gone to their website http://www.prototypejs.org? You can find some tutorials and tips there.
     
    mps705, Nov 18, 2009 IP
  4. dzflip

    dzflip Banned

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Do you know jquery ? It's a powerful js framework for everything you needed.
     
    dzflip, Nov 18, 2009 IP
  5. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #5
    If you are trying to update 'divid' only,
    referring to http://mootorial.com/wiki/mootorial/07-request/01-request.html,
    shouldn't your code be just like as follows:

    var request = new Request.HTML({
    url: '/live.php',
    method: 'get',
    update: 'divid',
    // onComplete: function(response) {
    //$('/live').set('php',response);
    //}
    })
     
    hdewantara, Nov 18, 2009 IP
  6. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I have no idea :p I'll try it :)
     
    Skillman13, Nov 18, 2009 IP
  7. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Can anyone give me instructions on where to put this code? what to name it? and what code to put in index.php or live.php?
     
    Skillman13, Nov 18, 2009 IP
  8. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #8
    Actually I am in the same position as you are Skillman13,
    a greenhorn in mootools too :D.

    But I suppose I could learn new world of javascripting (mootools, jQuery, Ajax etc) by helping you.
    Please consult a mootools expert after testing my scripts,

    There are two files attached, INDEX.PHP and LIVE.PHP,
    and I put them in the same folder as MOOTOOLS.JS on my server.

    Good luck.
     

    Attached Files:

    hdewantara, Nov 19, 2009 IP
  9. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #9
    erm.

    $('/live').set('php',response); -> this is wrong. the selector cant be /live - it's an invalid id. i doubt it would update anything.

    aside from that, the code you have is solid. keep in mind this works on the principles of:

    1. html markup
    <div id="live">content will be here that gets updated</div>

    2. creating a Request object called request in the global namespace and setting it to update the layer above

    3. creating a timed function that calls the request.send() method and repeats the request, updating in the process.


    there is only one thing i'd change as pointed, use update: $("live") and drop the onComplete event.

    also, consider using Request.periodical - http://mootools.net/docs/more/Request/Request.Periodical - it's a part of mootools-more
     
    dimitar christoff, Nov 19, 2009 IP