1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Fixing "undefined" problem

Discussion in 'JavaScript' started by redhits, Aug 9, 2010.

  1. #1
    So i made a script:

    http://proseductia.ro/keyword/

    The only problem with it it's this:

    If you write something really fast at keyword, it will display "undefined" for a few moments where it have to show the text... what i can do ? is there any way at less to change the "undefined" with processing?
     
    redhits, Aug 9, 2010 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi SeoPink,

    it is because of your function keycount() and the useage of AJAX there. First - if you use POST request, take away the last line http.send(null); - it's useful for GET request where you pass the query directly after URL. Second - you call http.abort(); before every new call, but later in http.onreadystatechange handler you check only for readyState, not for status. Aborted request has readyState=4, but it's status is never 200 (success), and therefore you get undefined into the textarea. It's simply not sending anything back to client...

    I rewrote it without testing, but I guess it will work;)
    
    function keycount(xkey,divname) {
    	var backtext;
    	var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
    	var sendText = document.getElementById('nicearea').value;
    	var sendKeyword = document.getElementById(xkey).value;
    
    	var key1 = document.getElementById('key1').value;
    	var key2 = document.getElementById('key2').value;
    	var key3 = document.getElementById('key3').value;
    
    	var query = "text="+escape(sendText)+"&keyword="+escape(sendKeyword)+"&key1="+escape(key1)+"&key2="+escape(key2)+"&key3="+escape(key3);
    
    	http.abort();
    	response='';
    	http.open("POST", "keycount.php", true);
    	http.setRequestHeader("Content-Type", contentType);
    	http.send(query);
    	http.onreadystatechange=function() {
    		if(http.readyState == 4 && http.status == 200) {
    			var response=http.responseText;
    			var excade=response.split("@#@phpguy@#@");
    			backtext=excade[1];
    			document.getElementById(divname).innerHTML = excade[0];
    			document.getElementById('inputtext').style.display="none";
    			document.getElementById('showtext').style.display="block";
    			document.getElementById('showtext').innerHTML = backtext;
    		}
    	}
    } 
    
    Code (Javascript):
    Btw. check JSON for sending well-formatted data with AJAX
     
    Last edited: Aug 9, 2010
    lp1051, Aug 9, 2010 IP