You are not logged in.

#1 2012-01-22 17:58:09

devadittya
Member
From: Bangalore
Registered: 2009-11-16
Posts: 127
Website

[SOLVED] Can't get Ajax working - help

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

#2 2012-01-22 18:18:10

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] Can't get Ajax working - help

Try initializing the xmlhttp variable smile

And some indentation might be nice.

Offline

#3 2012-01-22 18:32:59

devadittya
Member
From: Bangalore
Registered: 2009-11-16
Posts: 127
Website

Re: [SOLVED] Can't get Ajax working - help

Raynman wrote:

Try initializing the xmlhttp variable smile

And some indentation might be nice.

Ouch. My bad. That surely helped smile
I was just testing Ajax, so didn't bother much with indentation. Thanks a lot nonetheless smile

Offline

#4 2012-01-23 17:16:57

iTwenty
Member
From: India
Registered: 2010-10-24
Posts: 63
Website

Re: [SOLVED] Can't get Ajax working - help

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

Board footer

Powered by FluxBB