kartul Posted August 14, 2009 Share Posted August 14, 2009 i'm new to ajax. i've made a script that should return results from database but it keeps telling me just undefined. i googled and i know what the problem is, I just can't find it. i've stared the code for now about 15 minutes but I just can't see the problem here. var ajax = null; function showRes(wut) { if(wut.length == 0) { document.getElementById("results").innerHTML = ""; return; } if(window.XMLHttpRequest) { // IE7+, ff, chrome, opera, safari ajax = new XMLHttpRequest(); }else if(window.ActiveXObject) { // retards ajax = new ActiveXObject("Microsoft.XMLHTTP"); }else { alert("Your \"browser\" does not support AJAX!"); return; } var url = "search.php?q=" + wut; url = url + "&sid=" + Math.random(); ajax.open("GET", url, false); ajax.send(null); document.getElementById("results").innerHTML = ajax.reponseText; } Link to comment https://forums.phpfreaks.com/topic/170306-solved-script-returns-undefined/ Share on other sites More sharing options...
corbin Posted August 14, 2009 Share Posted August 14, 2009 Errrrr.... You need to read an AJAX tutorial >.<. When using AJAX asynchronously (how it's almost always used), you must use a callback function. Link to comment https://forums.phpfreaks.com/topic/170306-solved-script-returns-undefined/#findComment-898418 Share on other sites More sharing options...
kartul Posted August 14, 2009 Author Share Posted August 14, 2009 Errrrr.... You need to read an AJAX tutorial >.<. When using AJAX asynchronously (how it's almost always used), you must use a callback function. yea but... but this script works. var xmlhttp = null; function showRes(str) { if (str.length == 0) { document.getElementById("results").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); }else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }else { alert("Your browser does not support XMLHTTP!"); return; } var url = "search.php?q=" + str; url = url + "&sid=" + Math.random(); xmlhttp.open("GET",url,false); xmlhttp.send(null); document.getElementById("results").innerHTML = xmlhttp.responseText; } and they are almost identical, just one doesn't work. i got them from w3schools. so they teach it wrong? Link to comment https://forums.phpfreaks.com/topic/170306-solved-script-returns-undefined/#findComment-898424 Share on other sites More sharing options...
corbin Posted August 14, 2009 Share Posted August 14, 2009 ajax.open("GET", url, false); I actually didn't notice the false. The script is doing it synchronously. Hrmmm... Link to comment https://forums.phpfreaks.com/topic/170306-solved-script-returns-undefined/#findComment-898435 Share on other sites More sharing options...
kartul Posted August 15, 2009 Author Share Posted August 15, 2009 I decided to rewrite the code. I took the w3school code and changed a bit at a time. bottom line, got it to work. I still have no clue why it didn't work at the first place tho. thank you for trying to help.! Link to comment https://forums.phpfreaks.com/topic/170306-solved-script-returns-undefined/#findComment-899073 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.