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
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?

Link to comment
Share on other sites

Here...

 

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

 

Should be....

 

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

 

However, missedanswers will now be a Json object which you can't just simply print.

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.