galvin Posted January 19, 2011 Share Posted January 19, 2011 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/224899-sending-php-array-using-ajax/ Share on other sites More sharing options...
trq Posted January 19, 2011 Share Posted January 19, 2011 Even if $_SESSION['missedanswers'] is an array, you cannot simply echo it. May I suggest using json_encode to change your php array in json which is easily understood by Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/224899-sending-php-array-using-ajax/#findComment-1161644 Share on other sites More sharing options...
galvin Posted January 19, 2011 Author Share Posted January 19, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/224899-sending-php-array-using-ajax/#findComment-1161655 Share on other sites More sharing options...
trq Posted January 19, 2011 Share Posted January 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224899-sending-php-array-using-ajax/#findComment-1161657 Share on other sites More sharing options...
galvin Posted January 19, 2011 Author Share Posted January 19, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/224899-sending-php-array-using-ajax/#findComment-1161682 Share on other sites More sharing options...
trq Posted January 19, 2011 Share Posted January 19, 2011 //when game ends, send missed answers back to fill them in if (isset($_GET['endgame']) == "YES") { echo json_encode($_SESSION['missedanswers']); die(); } else { //do nothing and continue } Quote Link to comment https://forums.phpfreaks.com/topic/224899-sending-php-array-using-ajax/#findComment-1161690 Share on other sites More sharing options...
galvin Posted January 19, 2011 Author Share Posted January 19, 2011 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/224899-sending-php-array-using-ajax/#findComment-1161691 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.