Jump to content

[SOLVED] Function in onreadystatechange never being called


Lozan

Recommended Posts

Really, kind of annoying here. All I'm trying to do is when I click on an image, have it return some records and display it within a div. However, my onreadystate function is never called. Here is my code:

 

main.js

/* create XMLHttpRequest objects for retrieving most searched for games*/

require_once('async.js');
require_once('main.php');

function init(){

var ajaxObject = createAjaxObject();

if(ajaxObject)
{

	document.getElementById('morerecords').onclick = function(){

			ajaxObject.open('', 'http://localhost/main.php',true);
			ajaxObject.onreadystatechange = function(){
				getRandomRecords(ajaxObject);	
			}

			ajaxObject.send(null);

		}

}
}

function getRandomRecords(ajaxObject){

document.getElementById('results').innerHTML = "HI4";
if(ajaxObject.readyState == 4)
{
	if ((ajaxObject.status == 200) || (ajaxObject.status == 304))
	{
			var data = ajaxObject.responseXML;


			document.getElementById('results').innerHTML = 
					data.getElementsByTagName("Title")[0].childNodes[0].nodeValue;

	}
}

}

 

other php files:

 

main.php

<?php

require_once('main.class.php');

$main = new Main();
  
	echo $main->getRecords();	  

?> 

 

main.class.php

 

<?php

require_once('config.php');

class Main{

// database handler
public $mMysqli;

// constructor opens database connection
function __construct()
{
	// connect to the database
	$this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
}

// destructor closes database connection
public function __destruct()
{
	$this->mMysqli->close();
}

public function getRecords(){

	$query = 'SELECT Title FROM test';	

	$result = $this->mMysqli->query($query);

	header("Content-Type: text/xml");
	$output = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
	$output .= '<response>';

	if($result->num_rows)
	{

		while($row = $result->fetch_array(MYSQLI_ASSOC))
		{
			$output .= '<Title>' . $row['Title'] . '</Title>';
		}
		$this->mMysqli->close();
	}

	$output .= '</response>';

	return $output;
}

}

?>

 

 

Kind of fixed. I am now able to hit the function defined in onreadystatechange but am unable to grab anything from data (.responseXML).

 

When running just main.php, I get an error:

 

XML Parsing Error: junk after document element although, I see all the data I want:

 

Location: http://localhost/main.php
Line Number 1, Column 123:<?xml version="1.0" encoding="UTF-8" standalone="yes"?><response><Title>TestTitle</Title><Title>Batman</Title></response> <br />
--------------------------------------------------------------------------------------------------------------------------^

 

Does this error prevent anything from being returned? I current tried grabbing the title by doing:

 

data.getElementsByTagName("Title")[0].child[0].nodeValue;

 

and

 

data.getElementsByTagName("Title").child[0].nodeValue;

 

and

 

data.getElementsByTagName("Title").child.nodeValue;

 

and

 

data.getElementsByTagName("Title").nodeValue;

 

With no luck

 

Heh solved it, looks my persistence paid off. I had an extra mysqli->close which messed up the parsing. And then, I was just refering the node invalidly (I believe I just switched childNode to childNodes and it worked). So, I'm leaving this up and closing it if anyone else has similar issue.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.