Jump to content

Ajax not working


MDanz

Recommended Posts

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();
   
?>

Link to comment
https://forums.phpfreaks.com/topic/245319-ajax-not-working/
Share on other sites

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();
}

Link to comment
https://forums.phpfreaks.com/topic/245319-ajax-not-working/#findComment-1259981
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/245319-ajax-not-working/#findComment-1259987
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.