Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  169] [ 7]  / answers: 1 / hits: 30863  / 11 Years ago, fri, july 19, 2013, 12:00:00

i am trying to make an ajax commenting system where if a new comment is posted, the document title changes to (1) website title (like twitter)



my code is here



The xmlHTTPrequest



 function loadXMLDoc7(url)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.open(GET,url,false);
xmlhttp.send(null);
document.getElementById('newcomments').innerHTML=xmlhttp.responseText;
}


The PHP



    echo <script type='text/javascript'>
function auto2comments()
{
var MyDiv1 = document.getElementById('uiuiui');;
echo loadXMLDoc7(MyDiv1.innerHTML);
echo };
echo setInterval(auto2comments(),15000);</script>;
}


The DIV uiuiui contains /newcommentingi.php?show=0&id=username
The problem is when the Newcomments DIV gets filled, it shows

ID =

Show = 0

why?


More From » ajax

 Answers
32

The XmlHttpRequest object is asynchronous, which means that when it has the data ready, it returns it in a method. It is best to create a function to act as an event handler so that when the server responds, it calls the event handler function.



I think the solution you need is similar to here: How to get the response of XMLHttpRequest?


[#76875] Thursday, July 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zahrafrancisr

Total Points: 176
Total Questions: 105
Total Answers: 99

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;