MDanz Posted August 22, 2011 Share Posted August 22, 2011 AJAX only works once, i have to reload the page for it to work again. How do i fix this? echo "<div class='unsubscribe'><a id='us$id' href='javascript:unsubscribe($id);'><img src='/unsubscribe.jpg' alt='unsubscribe' /></a></div>"; function unsubscribe(number) { 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) { document.getElementById("us"+number).innerHTML="<img src='/subscribe.jpg' alt='subscribe' />"; } } xmlhttp.open("GET","/unsubscribe.php?id="+number,true); xmlhttp.send(); } Quote Link to comment https://forums.phpfreaks.com/topic/245431-ajax-onyl-works-once/ Share on other sites More sharing options...
Maq Posted August 22, 2011 Share Posted August 22, 2011 Do you get any errors in firebug or the js console after the first time? Quote Link to comment https://forums.phpfreaks.com/topic/245431-ajax-onyl-works-once/#findComment-1260542 Share on other sites More sharing options...
MDanz Posted August 22, 2011 Author Share Posted August 22, 2011 nope nothing wrong in firebug.. AJAX only works again when i reload the page. Quote Link to comment https://forums.phpfreaks.com/topic/245431-ajax-onyl-works-once/#findComment-1260555 Share on other sites More sharing options...
JasonLewis Posted August 22, 2011 Share Posted August 22, 2011 Perhaps it has something to do with creating multiple XMLHttpRequest objects? Try placing that code outside of the function so it's only run once. Quote Link to comment https://forums.phpfreaks.com/topic/245431-ajax-onyl-works-once/#findComment-1260610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.