Results 1 to 3 of 3

Thread: [RESOLVED] XMLHttpRequest retrieve variables?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Resolved [RESOLVED] XMLHttpRequest retrieve variables?

    what i'm trying to do is find all results that come after the page has loaded...

    the <div id="last">32</div> is the latest id that is displayed on the page, this i will have done with php later, but what i want to do, is when retrieving the data back from test.php i want this number to be replaced with $lastmessage which comes from test.php

    my question is: how do i retrieve the data as variables, so that i can replace the innerHTML of div id=last with only the variable $lastmessage, and add $messages to the div id=messages

    my code:

    index.php

    HTML Code:
    <script type="text/javascript">
    	function refreshMessages(lastID) {
    		if (lastID.length==0) {
    			document.getElementById("messages").innerHTML="";
    		return;  
    		}
    		if (window.XMLHttpRequest) {
    			xmlHttp=new XMLHttpRequest();  
    		} else {  
    			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");  
    		}
    		var url="test.php?q=" + lastID;
    		xmlHttp.open("GET",url,false);
    		xmlHttp.send();
    		e = document.getElementById("messages");
    		e.innerHTML=e.innerHTML+xmlHttp.responseText;
    	}
    </script>
    <div id="last">32</div>
    <input type="button" onclick="refreshMessages(document.getElementById('last').innerHTML);" value="Click"/>
    <div id="messages"></div>
    test.php

    PHP Code:
    <?php
    include("config.php"); // config.php only connects to the database

    $query_string mysql_real_escape_string($_GET['q']);

    $res mysql_query("SELECT * FROM chat WHERE id>'" $query_string "'") or die(mysql_error());
    $rowCount mysql_num_rows($res);

    if (
    $rowCount 1) {
    } else {
    $messages ""// define the variable
        
    while($row mysql_fetch_array($res)) {
        
    $uid $row['userid'];
            
    $res2 mysql_query("SELECT * FROM login WHERE uid='" $uid "'") or die(mysql_error());
            while(
    $row2 mysql_fetch_array($res2)) {
            
    $username $row2['display'];
            }
        
    $time $row['date'];
        
    $message $row['message'];
        
    $messages .= '<p style="padding:0px;margin:0px;margin-top:3px;width:380px;text-indent:5px;">' $username ' - [<font style="font-size:11px;">' date("H:i - d/m"$row['date']) . '</font>]<br /><font style="margin-left:10px;">' $row['message'] . '</font></p>';
        
    $lastmessage $row['id'];
        }
        echo 
    $messages;
    }
    ?>

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: XMLHttpRequest retrieve variables?

    Use PHP's json_encode() to return your data as JSON:
    PHP Code:
        //at the end of your code:
        //echo $messages;
        
    echo json_encode(array('lastmessage'=>$lastmessage'messages'=>$messages));
    }
    ?> 
    This will echo something like:
    Code:
    {"lastmessage":"32","messages":"<p>...</p>"}
    ...which Javascript can make use of like this:
    Code:
    var jsonResponse = JSON.parse(xmlHttp.responseText);
    alert(jsonResponse['lastmessage']); //32

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: XMLHttpRequest retrieve variables?

    perfect! thanks, just what i needed, works like a charm.

    apparently i can't rate the post

    edit: sorry forgot to mark as resolved
    Last edited by Justa Lol; Sep 6th, 2011 at 02:14 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width