Hi All!
I'm trying to populate an array with the results of the the request. I keep getting the error catch "Response failed". I want to send 5 requests and fill the array after each request. The database code works, I tested it in the database(MySQL) and I get a result there. It just doesn't seem to want to populate the array on each iteration. I had a different setup that output the results but it was buggy, so I thought I'd populate an array so I don't have the delay of sending a request each time I want to populate the DIV.
Any thoughts would be greatly appreciated.
Here's the AJAX code. Then the PHP code.
<SCRIPT language=JavaScript type="text/javascript">
var http = createRequestObject();
var i = 1; //global var
var scripts = new Array(); // blobal array to hold the responses from the requests
function createRequestObject() {
// find the correct xmlHTTP, works with IE, FF and Opera
var xmlhttp;
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
xmlhttp=null;
}
}
if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
xmlhttp=new XMLHttpRequest();
}
return xmlhttp;
}
// Make the XMLHttpRequest object
function sendRequest() {
// Open PHP script for requests
try{
var page = "testimo_query.php?num=" + i;
http.open("GET", page, true);
http.setRequestHeader('Content-Type', "text/text");
http.onreadystatechange = setArray;
http.send(null);
}
catch(e){
// caught an error
alert('Request send failed.');
}
}
function setArray(){
try{
if(http.readyState == 4){
scripts[i] = http.responseText;
i++;
if (i<6){
sendRequest();
}else { handleresponse();}
}
}
catch(e){
// caught an error
alert('Response failed.');
}
}
function handleResponse() {
for(j=1; j<6;j++){
var response = scripts[j];
Effect.Appear('ajaxDiv', { duration: 1.0 });
// UPDATE ajaxTest content
document.getElementById("ajaxDiv").innerHTML = response;
setTimeout(Effect.Fade('ajaxDiv', { duration: 1.0 }),5000);
}
}
window.onload=function() {
sendRequest();
}
</script>
PHP code
<?php
include("inc_openConnection.php");
$rec = $_GET['num']; //used to grab specific record
$rec= $rec-1; // want to start with record ZERO
$start = 1; // this is the limit of returned query records
$sql = "select fName, lName,date,state,country,testimonial from testimonials where approved = 1 order by t_id desc limit $rec,$start";
$rs = mysql_query($sql);
$row = mysql_fetch_array($rs);
$fn = $row['fName'];
$ln = $row['lName'];
$date = $row['date'];
$state = $row['state'];
$c = $row['country'];
$t = $row['testimonial'];
$response = "<font color='blue'><p>";
$response .= "\"";
$response .= $t;
$response .= "\"</p></font><font size='1'>";
$response .= $fn;
$response .= " ";
$response .= $ln;
$response .= "<br />";
$response .= $state;
$response .= ",";
$response .= $c;
$response .= "<br />";
$response .= $date;
$response .= "<br /></font><br /><font size=1 align='right'><a href='testimonials.php'>See more...</a></font></p>";
echo "$response";
?>