Jump to content

XML file returned empty


Rhysickle

Recommended Posts

I'm trying out some AJAX type stuff for the first time.

 

I've more or less copied the code from the sitepoint javascript anthology book, which shoudl be foolproof, but just can't get it to work.

 

I've attached the js and php that generates the response as a zip file.

 

A few things I've found out so far:

 

  1. If I have "" around the GET url I get a 403 error

  2. If I get rid of the "" (which isn't right, surely) I don't get an error, and the request processes successfully, but the XML returned is empty

  3. If I access the php file and pass it a variabe manually it creates the XML file ok

 

 

I'm running a standard install of XAMPP on my local machine.

 

Please please help as it's driving me mad

 

Rhys

 

[attachment deleted by admin]

Link to comment
Share on other sites

next time...put the code right in the post. and wrap it in code tags (it's the button with the # sign on it)

 

 

i see problems in both the JS and the PHP....

 

let's start with the JS: the following line:

requester.open("GET", "returnMonth.php?month=$month");

won't do what you want cus $month isn't a variable. variables in JS are done differently then PHP. it's apparent you are trying to pass a month here, but where is the value of the month coming from? the above will the URL with a month value of '$month', not something like '7' or 'July', which i assume is what you are looking for. this is why you are getting empty values back.

 

in the PHP, you need to add a Content-type to the response so your JS knows that what you are sending is XML. also, you can make the code much simpler by adding a WHERE to the mysql query instead of testing for it. furthermore, you are testing to see if ID is the same as $_GET['month']. Shouldn't you test to see if MONTH is the same as $_GET['month'] ??

<?php
header('Content-type: text/xml');
require_once ('../../connectors/mysqlconnect.php');
$month = mysql_real_escape_string($_GET['month']);
$monthly_birds = @mysql_query("SELECT month, wrexham, newales, id  FROM T_month_birds WHERE month = '{$month}'");
if($monthly_birds && mysql_num_rows($monthly_birds)) {
  $monthly_birds_row = mysql_fetch_array($monthly_birds);
  $wrexham = $monthly_birds_row['wrexham'];
  $newales = $monthly_birds_row['newales'];
}
echo "<?xml version=\"1.0\" ?>";	
echo "<birds>";
echo "<wrexham>";
echo $wrexham;
echo "</wrexham>";
echo "<newales>";
echo $newales;
echo "</newales>";	
echo "</birds>";
?>

Link to comment
Share on other sites

I've got a feeling I attached a previous version of the js as i fixed that $month problem a while back.

 

I reckon the header('Content-type: text/xml') is the important bit I'm missing - I've got a couple of ajax books and neither of them mention it!

 

Thanks for the answer

 

Rhys

Link to comment
Share on other sites

yeah...i tried your code without the text/xml and it was reading the response as text (WinXP + FF)

 

also, in your JS, on success your code doesn't really do anything...just sets two variables:

function success(requester)
{
  var wrexham = requester.responseXML.getElementsByTagName("wrexham");
  var newales = requester.responseXML.getElementsByTagName("newales");
  
  return true;
}

if you want something that will tell you if the code is working, try this:

function success(requester)
{
  var wrexham = requester.responseXML.getElementsByTagName("wrexham");
  alert(wrexham[0].childNodes[0].nodeValue);
  var newales = requester.responseXML.getElementsByTagName("newales");
  alert(newales[0].childNodes[0].nodeValue);
  
  return true;
}

Link to comment
Share on other sites

A bit of tweaking based on your advice solved it for firefox... so thanks again for your help.. but it still doesn't work in ie6.

 

I get error message line 240, "null" is null or not an object. Any ideas?

 

addLoadListener(createLinks);
addLoadListener(prevNextClicks);
addLoadListener(adaptMonths)
var today = new Date();
var monthCookie;
var month = today.getMonth() + 1;

function adaptMonths() {
if (getCookie("month")) {
month = getCookie("month");
}
initXMLHR()
}


function createLinks()
{
  var otherMonths = document.getElementById("otherMonths");
  var toRemove = otherMonths.firstChild;
  var removedChild = otherMonths.removeChild(toRemove);
  var prevMonth = document.createElement("a");
  prevMonth.href ="#";
  prevMonth.title ="Previous month's birds";
  prevMonth.id ="prevMonth";
  var prevMonthText = document.createTextNode("Prev");
  prevMonth.appendChild(prevMonthText)
  

  
  var nextMonth = document.createElement("a");
  nextMonth.href ="#";
  nextMonth.title ="Next month's birds";
  nextMonth.id ="nextMonth";
  var nextMonthText = document.createTextNode("Next");
  nextMonth.appendChild(nextMonthText)
  
  var thisMonth = document.createElement("a");
  thisMonth.href ="#";
  thisMonth.title ="Back to this month's birds";
  thisMonth.id ="thisMonth";
  var thisMonthText = document.createTextNode("Back to this month's birds");
  thisMonth.appendChild(thisMonthText)

  otherMonths.appendChild(prevMonth);
  otherMonths.appendChild(nextMonth);
  otherMonths.appendChild(thisMonth);
  
  return true;
  
  
  
}

function prevNextClicks()
{
  var nextmonth = document.getElementById("nextMonth");
  var prevmonth = document.getElementById("prevMonth");
  var thismonth = document.getElementById("thisMonth");
  
  attachEventListener(nextmonth, "click", next, false);
  attachEventListener(prevmonth, "click", prev, false);
  attachEventListener(thismonth, "click", thisMonth, false);
  return true;
}
var numbers
function next(event)
{
  if (typeof event == "undefined")
  {
    event = window.event;
  }
  month = ((month)%12)+1;
monthCookie = "month="+month;
    document.cookie = monthCookie;
  initXMLHR();

stopDefaultAction(event)
    return false;

}

function prev(event)
{
  if (typeof event == "undefined")
  {
    event = window.event;
  }
  month = month -2
  while (month<0) {
month+= 12;
  }
  month=((month)%12) +1;
monthCookie = "month="+month;
    document.cookie = monthCookie;
  initXMLHR();
  stopDefaultAction(event)
  return false;
  
  }


function thisMonth(event)
{
  if (typeof event == "undefined")
  {
    event = window.event;
  }
  month = today.getMonth() + 1;
  initXMLHR();
  stopDefaultAction(event)
  return false;
  
  } 
  
  
function stopDefaultAction(event)
{
  event.returnValue = false;

  if (typeof event.preventDefault != "undefined")
  {
    event.preventDefault();
  }

  return true; 
}



function initXMLHR()
{
  var requester;
  try
  {
    requester = new XMLHttpRequest();
  }
  catch (error)
  {
    try
    {
      requester = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (error)
    {
      requester = null;
    }
  }

  if (requester != null)
  {
    requester.onreadystatechange = function()
    {
      if (requester.readyState == 4)
      {
        if (requester.status == 200 || requester.status == 304)
        {
          success(requester);
        }
        else
        {
          failure(requester);
        }
      }

      return true;
    }

var get = "returnMonth.php?month=" + month;
requester.open("GET", get);
    requester.send(null);
  }
  else
  {
    return false;
  }

  return true;
}

function success(requester)
{
  var wrexham = requester.responseXML.getElementsByTagName("wrexham")[0].childNodes[0].nodeValue;
  var newales = requester.responseXML.getElementsByTagName("newales")[0].childNodes[0].nodeValue;
  var wrexhamBirds = document.getElementById("wrexhamBirds");
  var newalesBirds = document.getElementById("newalesBirds");
  
  var removeWrexham = wrexhamBirds.firstChild;
  wrexhamBirds.removeChild(removeWrexham);
  var newWrexhamText = document.createTextNode(wrexham);
  wrexhamBirds.appendChild(newWrexhamText)
  var removeNewales = newalesBirds.firstChild;
  newalesBirds.removeChild(removeNewales)
  var newNewalesText = document.createTextNode(newales);
  newalesBirds.appendChild(newNewalesText)  
  return true;
}

function failure(requester)
{
  alert("The XMLHttpRequest failed with status code: " + requester.status);

  return true;
}

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}

function attachEventListener(target, eventType, functionRef, capture)
{
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
    target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true; 
}

function getCookie(searchName)
{
  var cookies = document.cookie.split(";");

  for (var i = 0; i < cookies.length; i++)
  {
    var cookieCrumbs = cookies[i].split("=");
    var cookieName = cookieCrumbs[0];
    var cookieValue = cookieCrumbs[1];

    if (cookieName == searchName)
    {
      return cookieValue;
    }
  }
  return false;
}

 

 

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.