Hi all,

I'm sorry if the thread title doesn't make much sense; I'm quite new to AJAX (well, to web programming as a whole. I'm more of a SQL/client-server developer). Anyway, I'm having some trouble getting some code to work.

What I'm doing is I am dynamically loading a list of "products" into an <li> with an onclick event. The onclick will call a javascript function that makes use of AJAX to pass it into a PHP page to return the result. Here's my code (snippets. Let me know if you need more infomation):

//This is the code that dynamically builds the product list.
//This builds the string just fine and it displays fine on the page. 
//From what I can tell, this code should be fine.
$tmpParse = $tmpParse . "<li onclick=\"loadItem(\"" . $productID . "\")\" style=\"cursor:pointer\"><img src=\"" . $thumbnail . $productID . ".jpg\" alt=\"" . $productID . "\" width=\"45\" height=\"70\"/><a>Price: $" . $cost . "</a></li>";

//This is the loadItem function in my .js
function loadItem(productID)
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	var JSONObject = JSON.parse(xmlhttp.responseText);
	document.getElementById("title").innerHTML = JSONObject.title;
var url="itemDetails.php";
url = url+"?productID="+productID;

msg = url;

//And finally, this is the itemDetails PHP page where I extract the parameter:

$productID = $_GET['productID'];

I'm not quite sure what is wrong with the code. When I click the <li> on the page, it appears that the javascript isn't even being fired (I tried to put an "msg = productID" in it and I'm not getting the pop-up message). Nothing returns at all and the page just sits. What am I doing wrong?

Thanks in advance!

5 Years
Discussion Span
Last Post by fmjnax

I should also note that if I change the function that is built in the first code snippet to a different function that I know is working, it DOES call that other function... so I know the onclick is working in the <li>. I just can't get it to work with the function that I need it to use.


Well, it looks like I posted too soon. I downloaded the Firebug addon for Firefox and saw that JSON was returning an error (unexpected character). After examining my itemDetails.php page further, I found the problem and resolved it. Everything is displaying just fine now.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.