Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I am having trouble looking for the error in my program and I have this code but I do not understand it. Can someone explain it to me. Thanks in advance.

JavaScript
function addItem(str) {
	  if (str=="") {
		document.getElementById("txtHint").innerHTML="";
		return;
	  } 
	  if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	  } else { // code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
		  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		}
	  }
	  xmlhttp.open("GET","checkinout-actions/add-more-items-code.php?q="+str,true);
	  xmlhttp.send();
	  window.location.reload();
	}
Posted
Comments
Sergey Alexandrovich Kryukov 22-Apr-15 23:09pm    
Explaining code makes no sense at all, unless you ask some specific questions. We don't know your level, so, in general, it could be equivalent to teaching you whole JavaScript and programming, which hardly would be practical. Specific questions, please.
—SA

The is a javascript function that takes an argument called "str".
This code will clear the value of an html element called "txtHint" and return if the argument is empty:
C#
if (str=="") {
  document.getElementById("txtHint").innerHTML="";
  return;
}

Otherwise, the second part of the code is passing this argument as query string to a server-side script called "add-more-items-code.php" using Ajax method. The response from this server-side script will be displayed in "txtHint". You can learn about Ajax by going through this tutorial:
http://www.w3schools.com/ajax/default.asp[^]
The last line of code reload your browser, this will also clear the value in the "txtHint". If that is not what you want, then comment it out like this:
//window.location.reload();
 
Share this answer
 
v4
Comments
Michelle Anne E. Rigor 22-Apr-15 23:20pm    
Thanks for this explanation. :)
This link at the site Peter Leow listed might help you.

http://www.w3schools.com/php/php_ajax_php.asp[^]
 
Share this answer
 
v2
Please see my comment to the question.

Just in case, two hints:

The call window.XMLHttpRequest means AJAX: http://en.wikipedia.org/wiki/Ajax_(programming)[^].

And the call xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") means one of the biggest abuse one can possibly do using JavaScript, as well as assuming that somebody would use IE5 or IE6. Even Microsoft advised to completely deny serving any Web content to such users, probably purely hypothetical. Using ActiveXObject is utterly unsafe, too, and "real" browser won't support it.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900