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!