Hello all,
I have a problem with some AJAX code that I'm working on. There are three different parts to my problem. The goal of this code is to have a a link that says, "Show comments" to display the comments for a post from an admin underneath the update. Hopefully the code below will help clarify.
This is the code for the loop on the index page that actually displays the update and the line that says Show Comment. This file has imported the viewComment.js file so that is not the problem.
echo '<div id="pagename"><h3>Updates</h3></div><br />';
echo '<ul>';
while($update_array = mysqli_fetch_array($update_data)){
$poster_query = "SELECT username FROM wwit_user WHERE user_id=".$update_array['amsg_poster_id']."";
$poster_data = mysqli_query($dbc, $poster_query);
$username = mysqli_fetch_array($poster_data);
echo '<li><div id="update">'. $update_array['amsg_text'] . '</div> <div id="time">'. $update_array['amsg_post_time'] . ' - Posted by <a href="viewprofile.php?user_id='.$update_array['amsg_poster_id'].'">'. $username['username'] .'</a></div></li>';
?> <a href="#" onClick = "alert('<?php echo $update_array['amsg_id']; ?>'); startRequest('<?php echo $update_array['amsg_id']; ?>');">Show Comments</a> <?php
echo 'comments_'. $update_array['amsg_id'];
echo '<div id="comments_'. $update_array['amsg_id'] .'" class = "comments"></div>';
echo '<br />';
}
echo '</ul></div><br /><br />';
Next is the code in the viewComment.js that actually handles the xmlHttpRequest. When the Show Comment line from above is clicked, it calls the startRequest() function. Pardon all of the alert() functions. I've been using them to see what part is failing, and so far everything works up until a line in the displayComment() function that I've commented on below.
var xmlHttp;
var requestType = "";
var id;
var div_id;
var url;
function createXMLHttpRequest(){
alert("something's working");
if (window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
alert("work");
}
else if (window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
alert("You're using firefox/chrome/some other cool open-source browser");
}
else{
alert("failure");
}
}
function startRequest(parentId){
alert("work");
id = parentId;
alert(id);
createXMLHttpRequest();
alert("keep checking");
xmlHttp.onreadystatechange = handleStateChange;
url = "/view_comment.php?parent_id=" + id;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
alert("COOL BEANS " + id);
displayComments(id);
alert("I LOVE JAVASCRIPT");
}
}
}
function displayComments(){
div_id = "comments_" + id;
alert(div_id);
var comments = document.getElementById(div_id);
alert("get div_id works");
//Here is where I run into the problem. The alert function above
//displays but not the one below that says "get comment works"
//I think there is a problem with something in the line below
//with the results variable
var results = xmlHttp.responseXML.getElementsByTagName("comment");
alert("get comment works");
var user = null;
var div = null;
var url2 = null;
alert("somethings working");
for(var i=0; i< results.length; i++){
div = document.createElement("div");
div.setAttribute('id', "comment");
user = document.createElement("a");
url2 = "/viewprofile.php?user_id=" + results[i].childNode[2].nodeValue;
user.setAttribute('href', url2);
user.nodeValue = results[i].childNode[2].nodeValue;
text = document.createElement("p");
text.setAttribute('class', 'comment_text');
text.nodeValue = results[i].childNode[3].nodeValue;
time = document.createElement("p");
time.setAttribute("id", "comment_time");
time.nodeValue = results[i].childNode[4].nodeValue;
div.appendChild(user);
div.appendChild(text);
div.appendChild(time);
comments.appendChild(div);
}
}
Finally I have a php script to return the data values in xml format. I have not used xml a lot in the past so I think this is where I might have screwed up.
<?php
$parent_id = $_GET['parent_id'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$comment_query = "SELECT * FROM comments WHERE parent_id = '$parent_id' ORDER BY post_time ASC";
$comment_data = mysqli_query($dbc, $comment_query);
$comment_rows = mysqli_fetch_array($comment_data);
xmlOutput = '<comments>';
while($comment_rows){
xmlOutput .= '<comment>';
xmlOutput .= '<comment_id>'. $comment_rows['comment_id'] .'</comment_id>';
xmlOutput .= '<poster_id>'. $comment_rows['poster_id'] . '</poster_id>';
//Get username
$username_query = "SELECT username FROM wwit_user WHERE user_id = '$comment_rows['poster_id']'";
$username_data = mysqli_query($dbc, $username_query);
$username_rows = mysqli_fetch_array($username_data);
xmlOutput .= '<poster_name>'. $username_rows['username'] .'</poster_name>';
xmlOutput .= '<comment_text>'. $comment_rows['comment_text'] .'</comment_text>';
xmlOutput .= '<comment_time>'. $comment_rows['post_time'] .'</comment_time>';
xmlOutput .= '</comment>';
}
xmlOutput .= '</comments>';
echo xmlOutput;
?>
Sorry for throwing so much out at once, but I can't seem to find the problem. Hopefully a fresh pair of eyes will be able to find the problem. Thank you very much.