Jump to content

[SOLVED] Script returns: undefined


kartul

Recommended Posts

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

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?

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.