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();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{		
	var JSONObject = JSON.parse(xmlhttp.responseText);
			
	document.getElementById("title").innerHTML = JSONObject.title;
	document.getElementById("byline").innerHTML=JSONObject.byline;
	document.getElementById("body").innerHTML=JSONObject.msgbody;
	}
  }
  
var url="itemDetails.php";
 
url = url+"?productID="+productID;

msg = url;
  
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}


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

$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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.