Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Thanked 0 Times in 0 Posts

    Question refresh part of a page.

    Hi everyone,

    i have this webpage that is storing messages posted into a database. what i'm trying to do is:

    check how many messages are in the database on load, then have a loop that every 10 seconds will check if there are more messages, if so display them, if not start the loop again.

    i was looking to do this with php and use mysql_num_rows($query) to check the rows but couldn't figure out how to get the loop.

    so i have been directed to use AJAX to do it.

    any idea's on where to start? i have read the tutorial on ajax at w3school.com, but that's about as far as my AJAX knowledge goes...

  • #2
    Regular Coder godofreality's Avatar
    Join Date
    Jan 2009
    Thanked 15 Times in 15 Posts
    this should do it

    PHP Code:
    $result mysql_query("your query data here");
    $rows mysql_num_rows($result);
    setInterval ("check()",10000);
    function check() 
    var trick = Math.floor(Math.random()*100000);
    var page = "*****insert into this spot the name of the php file that has the query on it*****";
    	var XMLHttp = false;
    	if (window.XMLHttpRequest)
    			XMLHttp = new XMLHttpRequest();
    	else if (window.ActiveXObject) 
    			XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    	if (XMLHttp) 
    			XMLHttp.open("POST", page+"?"+trick);
    			XMLHttp.onreadystatechange = function() 
    				if (XMLHttp.readyState==4) 
    var amount = document.getElementById('message_quantity').innerHTML;
    if(XMLHttp.responseText > amount)
    document.getElementById('message_quantity').innerHTML = if(XMLHttp.responseText;
    /* place 
    <span id="message_quantity"></span>
    onto your html page where you would like it to display the amount of messages
    and your <body> tag should be <body onload="check();">


    Posting Permissions

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