javascript - Handling Multiple Ajax Request -
how handle multiple ajax request
i using more 1 like button in single php page , when clicked calls same ajax code update corresponding like unlike text..
the below code works fine button when click of them n waits till ajax update it.. when click more 1 @ same time , waits updation, in such condition last clicked text changes unlike..
please provide better solution or code it
thanx
page : like.php
<span id="like1" onclick="ajaxfun(1)">like</span><br /> <span id="like2" onclick="ajaxfun(2)">like</span><br /> <span id="like3" onclick="ajaxfun(3)">like</span><br /> <span id="like4" onclick="ajaxfun(4)">like</span><br /> <span id="like5" onclick="ajaxfun(5)">like</span><br /> .... <span id="like10" onclick="ajaxfun(10)">like</span><br />
page : ajaxx.js
function ajaxfun(id) { document.getelementbyid("like"+id).innerhtml="wait !!!"; if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { var getdata=xmlhttp.responsetext; if(getdata=="ok") { document.getelementbyid("like"+id).innerhtml="unlike"; } else { document.getelementbyid("like"+id).innerhtml="like"; } } } xmlhttp.open("get","verify.php?id="+id,true); xmlhttp.send(); }
page : verify.php
it verify , if done returns ok else not ok
error :(
like like wait !!! wait !!! wait !!! wait !!! wait !!! wait !!! unlike
just set array ajax handlers...
var arrajaxhandlers = []; function ajaxfun(id) { document.getelementbyid("like"+id).innerhtml="wait !!!"; if ( arrajaxhandlers[ "like"+id ] == null ) { if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari arrajaxhandlers[ "like"+id ]=new xmlhttprequest(); } else { // code ie6, ie5 arrajaxhandlers[ "like"+id ]=new activexobject("microsoft.xmlhttp"); } } arrajaxhandlers[ "like"+id ].onreadystatechange=function() { if (arrajaxhandlers[ "like"+id ].readystate==4 && arrajaxhandlers[ "like"+id ].status==200) { var getdata=arrajaxhandlers[ "like"+id ].responsetext; if(getdata=="ok") { document.getelementbyid("like"+id).innerhtml="unlike"; } else { document.getelementbyid("like"+id).innerhtml="like"; } } } arrajaxhandlers[ "like"+id ].open("get","verify.php?id="+id,true); arrajaxhandlers[ "like"+id ].send(); }