0

I'm doing an assigment for my programming class. I need to show a table with two columns (name and lastname) of a list of friends. This data is stored in a XML file like this:

<?xml version='1.0' standalone='yes'?>
<amigos>
 <amigo>
  <nombre>Alejandra</nombre>
  <apellido>Ponce</apellido>
 </amigo>
 <amigo>
  <nombre>Dalia</nombre>
  <apellido>Gordon</apellido>  
 </amigo>

I retrieve this data with php, like this:

<table width="200" border="1">
 <tr align="center">
    <td>NOMBRE</td>
    <td>APELLIDO</td>
 </tr>

<?php
$amigos = simplexml_load_file('listaamigos.xml');

foreach ($amigos->amigo as $amigo) {
    echo '<tr>';
    echo '<td>',$amigo->nombre,'</td>';
    echo '<td>',$amigo->apellido,'</td>';
    echo '</tr>';
}
?>

I call this php file with a function written in javascript language. I'm using ajax for this homework. My javascript file is the one I'm showing below:

var xmlHttp;
function  createXmlHttpRequestObject() {

    if(window.ActiveXObject){
        try{
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e)
        {
            xmlHttp=false;
        }
    }
    else{
        try{
            xmlHttp = new XMLHttpRequest();
        }catch(e)
        {
            xmlHttp=false;
        }
    }

    if(!xmlHttp)
        alert('Can't connect to the host');
    else
        return xmlHttp; 
}

function cargarAmigos(url) 
{
  if(url=='')
  {
    return;
  }
  xmlHttp=createXmlHttpRequestObject();
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = procesarEventos;
  xmlHttp.send(null);
}

function cargar(url) 
{
  if(url=='')
  {
    return;
  }
  xmlHttp=createXmlHttpRequestObject();
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = procesarEventos;
  xmlHttp.send(null);
}

function procesarEventos()
{
  if(xmlHttp.readyState == 4)
  {
      document.getElementById("listaAmigos").innerHTML= xmlHttp.responseText;
  } 
}

I use this javascript file in a html file. I call the function cargar () in the button's onclick event, like this:

<body>
 <div id="listaAmigos" > 
  Lista amigos
 </div>
 <button onclick="cargarAmigos('procedimiento.php');">Lista Amigos    Ajax</button>
</body>

The problem is that the code is not working. I mean that the names are not displaying at all in the table. Actually the table does not appear. I'm pretty sure that the error is not in the code because it was working last week and I haven't changed the code since then. But today I loaded the html page just for "fun" and it wasn't showing my list of friends. So I decided to open another project with uses the same algorithm and this one is not working too. I've checked that all the files are in the same folder. I've checked my xammp server and it's apache and mysql services are on. I have no idea what "thing" I could have done to cause this problem.

Any help will be really appreciated.

1 Answers1

0

It looks like you should escape the ' character in this line:

alert('Can't connect to the host');

i.e. rewrite it like

alert("Can't connect to the host");

You can see that there's an error even just by looking at the formatting on Stack Overflow :)

Please refer to this question for the details on why it's working like that: When to use double or single quotes in JavaScript?

Community
  • 1
  • 1
NikitaBaksalyar
  • 2,414
  • 1
  • 24
  • 27