I'm trying to recreate the Gmail 'star' favourite button using AJAX. Unfortunately what I have isn't working and I can't work out why.
I have the following in my HMTL:
<img id="item_1" src="/_images/star_off.gif" onclick="updateStar(this.id)" />
<img id="item_2" src="/_images/star_off.gif" onclick="updateStar(this.id)" />
And I'm using the following Javascript in a seperate file:
function updateStar(id) {
var imgsrc = (document.getElementById(id).src == "/_images/star_off.gif") ? "/_images/star_on.gif" : "/_images/star_off.gif";
var sendId = id.split('_')[1];
var sendStar = (imgsrc == "/_images/star_off.gif") ? false : true;
var objXml = new XMLHttpRequest();
var datasource = "favourite.php";
var params = "id=" + sendId + "&star=" + sendStar;
objXml.open("GET", datasource + "?" + params, true);
objXml.onreadystatechange=function() {
if ((objXml.readyState==4) && (objXml.status==200)) {
alert('status changed.');
}
}
objXml.send(null);
}
The script favourite.php sets inserts the id into a favourites database table (or removes it if star == false).
I can't see what is wrong here but it isn't working. I've also tried the suggestions on http://stackoverflow.com/questions/2563094/jquery-gmail-star but they wont work either. Any suggestions?