You are not logged in.
I am trying to set Ajax up for a media server (hosting professor lectures).
I am trying to update my mysql, if someone likes a particular lecture.
However, I am not able to get any response what so ever. I am using an onclick - however, nothing happens. Check the code below - I have set up an if-else, with onreadyState, and for both the conditions (exhaustive), no change happens. Please suggest what I am doing wrong.
function show_jw()
{
var filepath=jwplayer('container').getPlaylistItem().file;
var xmlhttp;
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("like").innerHTML="file found";
}
else
{
document.getElementById("like").innerHTML="file not found";
}
}
xmlhttp.open("GET","ajaxphp.php?f="+filepath,true);
xmlhttp.send();
}
</script>
<input type="button" onclick=show_jw() value="like" />
on clicking the button, I should either see "file found" or "file not found". I see nothing (on chrome or firefox).
Last edited by devadittya (2012-01-22 18:33:24)
Offline
Try initializing the xmlhttp variable
And some indentation might be nice.
Offline
Try initializing the xmlhttp variable
And some indentation might be nice.
Ouch. My bad. That surely helped
I was just testing Ajax, so didn't bother much with indentation. Thanks a lot nonetheless
Offline
Also note that the else statement gets executed even if readyState != 4 which is probably not what you want. Better to implement it as:
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
if (xmlhttp.status==200)
{
document.getElementById("like").innerHTML="file found";
}
else
{
document.getElementById("like").innerHTML="file not found";
}
}
}
More info here: http://www.w3.org/TR/2012/WD-XMLHttpReq … readystate. The entire page is a good read actually.
Last edited by iTwenty (2012-01-23 17:17:24)
“Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover.” - Mark Twain
Offline