简体   繁体   English

如何使用PHP从XML文件加载数据,以使用javascript在html页面中显示它

[英]How to load data from an XML file using php to show it in an html page with javascript

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. 我需要显示一个包含两列朋友列(name和lastname)的表。 This data is stored in a XML file like this: 此数据存储在XML文件中,如下所示:

<?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: 我用php检索这些数据,如下所示:

<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. 我用这个用javascript语言编写的函数调用这个php文件。 I'm using ajax for this homework. 我正在使用ajax做这个功课。 My javascript file is the one I'm showing below: 我的javascript文件是我在下面显示的文件:

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. 我在一个html文件中使用这个javascript文件。 I call the function cargar () in the button's onclick event, like this: 我在按钮的onclick事件中调用了函数cargar() ,如下所示:

<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. 但是今天我加载html页面只是为了“有趣”而且它没有显示我的朋友列表。 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. 我已经检查了我的xammp服务器,它的apache和mysql服务都已启用。 I have no idea what "thing" I could have done to cause this problem. 我不知道我能做什么“事情”导致这个问题。

Any help will be really appreciated. 任何帮助将非常感激。

It looks like you should escape the ' character in this line: 看起来你应该逃避这一行中的'角色:

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

ie 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 :) 你甚至可以通过查看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? 请参考这个问题,了解它为何如此工作的详细信息: 何时在JavaScript中使用双引号或单引号?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM