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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.