Jump to content

simple AJAX function


9three

Recommended Posts

Hey,

 

I have this function

 

var xmlHttp = GetXmlHttpObject();
function reportPhoto(div, photoID) {
  var URL = 'ajax/ajax.reportPhoto.php?photoID='+photoID;
  if (xmlHttp == null)
    return;

  xmlHttp.onreadystatechange = stateChanged(div);
  xmlHttp.open('POST', URL, true);
  xmlHttp.send(null);
}

 

Which initiates these two functions:

function GetXmlHttpObject() {
  if (window.XMLHttpRequest) {
    //IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject) {
    //IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}

function stateChanged(div) {
  if (xmlHttp.readyState == 4) {
    document.getElementById(div).innerHTML = xmlHttp.responseText;
  }
}

 

Problem is that it doesn't do anything. No errors are thrown. My table in my DB is empty. I'm running on the latest firefox.

 

Not sure what I'm doing wrong? I normally use Jquery but I wanted to switch over to this method instead.

 

EDIT:

 

I changed somethings around and now when I click on an href twice with that "reportPhoto" function it just disappears the div but nothing else happens.

Link to comment
Share on other sites

Ok so now it is sending the data to the database but it's not switching the HTML when its clicked.

 

var xmlHttp = GetXmlHttpObject();
function reportPhoto(div, photoID) {
  var URL = 'ajax/ajax.reportPhoto.php';
  var params = 'photoID=' + photoID;
  
  if (xmlHttp == null)
    return;
  
  xmlHttp.open("POST", URL, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");

xmlHttp.onreadystatechange = stateChanged(div);
xmlHttp.send(params);
}

function GetXmlHttpObject() {
  if (window.XMLHttpRequest) {
    //IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject) {
    //IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}

function stateChanged(div) {
  if (xmlHttp.readyState == 4) {
    parent.document.getElementById(div).innerHTML = xmlHttp.responseText;
  }
}

 

My PHP file echos out "Picture reported" if true else it prints "Error"

Link to comment
Share on other sites

Debug it step by step.  I assume it has something to do with the "parent" reference, but I'm unsure.

 

Start by making the parent.document.getElementById(div).innerHTML = xmlHttp.responseText; line instead say alert(xmlHttp.responseText); and go from there.

 

Test that it alerts the correct data.  Then test that you can change a direct reference to the HTML object (document.getElementById('nameofdiv').innerHTML = 'test').  Then test that the parent.document.getElementById(div) is actually getting the right element (and not erring).  Then see if the argument div is referencing the object correctly.

 

Try those things, otherwise we may need to see more code.

Link to comment
Share on other sites

I changed it to what you said. The alert doesn't go through.

 

function stateChanged(div) {
  if (xmlHttp.readyState == 4) {
    alert(xmlHttp.responseText);
    //parent.document.getElementById(div).innerHTML = xmlHttp.responseText;
  }
}

 

If the data went through then the readyState is 4. But it's not prompting the alert box... weird..

Link to comment
Share on other sites

If the data went through then the readyState is 4
Not necessarily...

This page shows a brief explanation of what codes are returned: http://www.w3schools.com/dom/dom_http.asp

If deals with the readystate as well as the page status (which you do not check in your code).

 

Anyway, continue to debug step by step.  Place debugging alerts backwards through your code until you find where it actually breaks.  ie:

function stateChanged(div) {
  alert('made it to stateChanged()');

  if (xmlHttp.readyState == 4) {
    alert('made it to readystate ==4');
    //parent.document.getElementById(div).innerHTML = xmlHttp.responseText;
  }else{
    alert('made it to readystate = ' + xmlHttp.readystate);
  }
}

Link to comment
Share on other sites

Well, if it doesn't go past 1, (it should go through them until 4 if it's successful) , then try checking the xmlHttp.status code and see if it is indeed working correctly (200).  You need to narrow down the problem since it seems to be between your script and the page it wants to query -- syntactically it seems correct.

Link to comment
Share on other sites

Hm, not sure if I'm doing it right but I'm getting an error

 

if (xmlHttp.readyState == 4) {
    alert('made it to readystate ==4');
    //parent.document.getElementById(div).innerHTML = xmlHttp.responseText;
  }else{
    alert('made it to readystate = ' + xmlHttp.readyState);
    alert('status: ' + xmlHttp.statusText);
  }

 

Tried using xmlHttp.status  as well

 

The quey IS being sent though.

Link to comment
Share on other sites

The PHP can echo out whatever you want it to.

 

if (xmlHttp.readyState == 4) {
    alert('made it to readystate ==4');
    //parent.document.getElementById(div).innerHTML = xmlHttp.responseText;
  }else{
    alert('made it to readystate = ' + xmlHttp.readyState);
    alert('status: ' + xmlHttp.statusText);
  }

 

That code should echo "made it to readystate ==4" if that part of the request made it...  Keep trying, I won't be able to assist more tonight, but I'm sure you can figure it out by checking it step by step.

Link to comment
Share on other sites

I tried:

 

function stateChanged(div) {
  if (xmlHttp.readyState == 4) {
    alert('made it to readystate ==4');
    //parent.document.getElementById(div).innerHTML = xmlHttp.responseText;
  }else{
    alert('made it to readystate = ' + xmlHttp.readyState);
    try {
    alert('status: ' + xmlHttp.statusText);
    }
    catch (e) {
      alert(e.description);
    }
  }
}

 

Gives me undefined :-/

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.