MDanz Posted August 20, 2011 Share Posted August 20, 2011 where did i go wrong? It should update mysql(insertsuscribe function) and change the image in the anchor tag. This is my first time doing AJAX, what did i do wrong? php $id= $row['id']; echo "<div class='suscribe'><a id='s$id' href='javascript:suscribe($id);'><img src='/suscribe.jpg' alt='suscribe' /></a></div>"; ajax function suscribe(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("s"+number).innerHTML="<img src='/unsuscribe.jpg' alt='unsuscribe' />"; } } xmlhttp.open("GET","suscribe.php?id="+number,true); xmlhttp.send(); } suscribe.php <?php session_start(); include "database.php"; $id = $_GET['id']; $database = new Database(); $database->opendb(); $database->insertsuscribe($id); $database->closedb(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/245319-ajax-not-working/ Share on other sites More sharing options...
MDanz Posted August 21, 2011 Author Share Posted August 21, 2011 i've simplified it. This should be simple. I click on the subscribe.jpg image and the image changes to unsubscribe.jpg. and then subscribe.php is called. But nothing is happening when i click the subscribe image?? echo "<a id='s1' href='javascript:subscribe(1)'><img src='/subscribe.jpg' alt='subscribe' /></a>"; function subscribe(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("s"+number).innerHTML="<img src='/unsubscribe.jpg' alt='unsubscribe' />"; } } xmlhttp.open("GET","subscribe.php?id="+number,true); xmlhttp.send(); } Quote Link to comment https://forums.phpfreaks.com/topic/245319-ajax-not-working/#findComment-1259981 Share on other sites More sharing options...
trq Posted August 21, 2011 Share Posted August 21, 2011 You should read this. Quote Link to comment https://forums.phpfreaks.com/topic/245319-ajax-not-working/#findComment-1259982 Share on other sites More sharing options...
MDanz Posted August 21, 2011 Author Share Posted August 21, 2011 so i should never use JavaScript with AJAX? always jQuery and AJAX? I was following a tutorial, is their a specific reason? Quote Link to comment https://forums.phpfreaks.com/topic/245319-ajax-not-working/#findComment-1259984 Share on other sites More sharing options...
trq Posted August 21, 2011 Share Posted August 21, 2011 I was following a tutorial, is their a specific reason? You could write your own Ajax functionality in JavaScript (as you have done), but it is much better to use tried and true implementations that are well tested, flexible and easy to use. Using a framework for JavaScript just takes allot of the grunt work out of it. Quote Link to comment https://forums.phpfreaks.com/topic/245319-ajax-not-working/#findComment-1259987 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.