Ajax not working?
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();
?>
Presumably the HMTL is valid. Ajax has issues if the html is not valid. Also if you are using firefox have you considered using a plugin such as https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/ . This will help you identify if the issue is with the data not being sent, received or the page not updating.
精彩评论