Jump to content

Sending PHP array using AJAX?


galvin

Recommended Posts

I have the AJAX code below which calls to a compare.php page which sends back a PHP Array using this code...

echo $_SESSION['missedanswers'];

 

When I run this, I only literally get "missedanswers" in my browser (in the "missedanswers" div).  Why am I getting that instead of the stuff in the PHP array?

 

Also, do I have "document.getElementById('missedanswers').innerHTML = "missedanswers"" in the wrong area of the code?

 

AJAX CODE:

function getmissedanswers() {

var xmlHttp;
try {  // Firefox, Opera 8.0+, Safari 
	xmlHttp=new XMLHttpRequest();
} catch (e) {  // Internet Explorer 
	try {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			alert("Your browser does not support AJAX!");
			xmlHttp=null;
		}
	}
}
if (xmlHttp !== null) {
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4 && xmlHttp.status==200) {
		var missedanswers = xmlHttp.responseText;

		}
	}
	xmlHttp.open("GET","includes/compare.php?endgame=YES", true);
	xmlHttp.send(null);

	document.getElementById('missedanswers').innerHTML = "missedanswers";		
}
}

 

Link to comment
https://forums.phpfreaks.com/topic/224899-sending-php-array-using-ajax/
Share on other sites

Ok, so now I am echoing back...

echo json_encode($_SESSION['missedanswers']);

but it still just says literally "missedanswers" in the browser, as a result of my AJAX code. I assume something's gotta be wrong with my syntax in the AJAX code, but I can't tell what.  Any ideas?

Ok I'll check out JSON in more depth.  One quick very newbie question.  If I have this code and the first code is run (i.e. it does equal "YES")...

 

//when game ends, send missed answers back to fill them in
if (isset($_GET['endgame']) == "YES") {
echo json_encode($_SESSION['missedanswers']);
} else {
//do nothing and continue
}

 

 

...how can I make it so no more code runs further down the page?  In plain english, i want this...

 

//when game ends, send missed answers back to fill them in
if (isset($_GET['endgame']) == "YES") {
echo json_encode($_SESSION['missedanswers']);
        (((DON"T RUN ANYMORE CODE ON THIS PAGE))
} else {
//do nothing and continue
}

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.