I am trying to use AJAX for the first time, and I am having a difficult time getting the hang of it. Here is the request code:
function requestData(j) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
xml = xmlhttp.responseXML;
post = "";
title = "";
postdata = xml.getElementsByTagName("post");
titledata = xml.getElementsByTagName("title");
datedata = xml.getElementsByTagName("date");
timedata = xml.getElementsByTagName("time");
document.getElementById("post").value = postdata[0].childNodes[0].nodeValue;
document.getElementById("heading").value = titledata[0].childNodes[0].nodeValue;
document.getElementById("date").value = datedata[0].childNodes[0].nodeValue;
document.getElementById("time").value = timedata[0].childNodes[0].nodeValue;
document.getElementById("id").value = j;
document.getElementById("update").value = "true";
}
};
xmlhttp.open("POST","../script/getnewsdata.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("ID=" + j);
return false;
}
Firebug is telling me that “xml is null” on the line postdata = xml.getElementsByTagName(“post”); Which means that xmlhttp.responseXML is null.
This is the server side script:
<?php
$db = mysql_connect("wadafw","awfawf","awfsgv");
if(!$db)
{
die("Could not connect: " . mysql_error());
}
mysql_select_db("afggbare", $db);
$updata = mysql_query('SELECT * FROM News WHERE NewsID='.$_POST['ID']);
$blog = mysql_fetch_array($updata);
$post = $blog['Content'];
$regex = Array('/<br />/', '/<(\/?)(b|i|u)>/', '/<a href="(http://[www.]?\w+)">(\w+)<\/a>/', '/<div class="media"><iframe title="YouTube video player" width="425" height="265" src="http://www.youtube.com/embed/(\w+)hd=1" frameborder="0" allowfullscreen></iframe></div>/', '/<div class="media"><img width="425" src="(http://[www.]?[\w+])" /></div>/');
$regReplace = Array('\r\n', '[$1$2]', '[link=$1]$2[/link]', '[youtube]http://www.youtube.com/watch?v=$1[/youtube]',
'[img]$1[/img]');
$post = preg_replace($regex, $regReplace, $post);
echo '<newsItem>
<title>'.$blog['Heading'].'</title>
<post>'.$post.'</post>
<date>'.$blog['time'].'</date>
<time>'.$blog['time'].'</time>
</newsItem>';
?>
The regex is probably bad… but that’s not important right now.
Changed to this… and now getting “function not defined” errors:
function requestData(j) {
$.ajax("../script/getnewsdata.php", {
data: {ID: j},
type: "POST",
dataType: "xml",
success: function(data, status, jqXHR){
var xml = jqXHR.responseXML;
postdata = xml.getElementsByTagName("post");
titledata = xml.getElementsByTagName("title");
datedata = xml.getElementsByTagName("date");
timedata = xml.getElementsByTagName("time");
document.getElementById("post").value = postdata[0].childNodes[0].nodeValue;
document.getElementById("heading").value = titledata[0].childNodes[0].nodeValue;
document.getElementById("date").value = datedata[0].childNodes[0].nodeValue;
document.getElementById("time").value = timedata[0].childNodes[0].nodeValue;
document.getElementById("id").value = j;
document.getElementById("update").value = "true";
}
}
}
Okay, found that problem. It was just a typo… hadn’t closed the $.ajax() parameter brackets. Now I am not getting errors. But it just doesn’t do anything…
Instead of trying to get AJAX running from scratch, I’d recommend using an existing library. jQuery makes this task extremely easy.
http://api.jquery.com/jQuery.ajax/