我有一个PHP生成的XML文件,我正在尝试使用Javascript。我的主要错误是我接触到Javascript它说“Uncaught TypeError:无法调用方法'getElementsByTagName'为null”我现在很多东西都是在线尝试复制/粘贴几个教程。我将包括三个块,首先在我的PHP脚本中,第二个是它输出的内容,第三个是javascript(这是我真的不知道的部分)。该项目的目标是将数据库表放入Javascript小程序。
if($id == "poll"){
//Need to report back everything
$check = mysql_query("SELECT * FROM `lightstatus`");
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('doc');
while ($row = mysql_fetch_assoc($check)) {
$xml->startElement("setting");
$xml->startElement("id");
$xml->writeRaw($row['id']);
$xml->endElement();
$xml->startElement("status");
$xml->writeRaw($row['status']);
$xml->endElement();
$xml->endElement();
}
$xml->endElement();
header('Content-type: text/xml');
$xml->flush();
}
^^这很好用
<?xml version="1.0"?>
<doc>
<setting>
<id>1</id>
<status>0</status>
</setting>
<setting>
<id>2</id>
<status>0</status>
</setting>
<setting>
<id>3</id>
<status>0</status>
</setting>
<setting>
<id>4</id>
<status>0</status>
</setting>
<setting>
<id>5</id>
<status>0</status>
</setting>
<setting>
<id>6</id>
<status>0</status>
</setting>
<setting>
<id>7</id>
<status>0</status>
</setting>
<setting>
<id>8</id>
<status>1</status>
</setting>
<setting>
<id>9</id>
<status>0</status>
</setting>
<setting>
<id>10</id>
<status>0</status>
</setting>
<setting>
<id>11</id>
<status>0</status>
</setting>
<setting>
<id>12</id>
<status>0</status>
</setting>
<setting>
<id>13</id>
<status>0</status>
</setting>
<setting>
<id>14</id>
<status>0</status>
</setting>
<setting>
<id>15</id>
<status>0</status>
</setting>
<setting>
<id>16</id>
<status>0</status>
</setting>
<setting>
<id>17</id>
<status>0</status>
</setting>
<setting>
<id>18</id>
<status>0</status>
</setting>
<setting>
<id>19</id>
<status>0</status>
</setting>
<setting>
<id>20</id>
<status>0</status>
</setting>
<setting>
<id>21</id>
<status>0</status>
</setting>
<setting>
<id>22</id>
<status>0</status>
</setting>
<setting>
<id>47</id>
<status>1</status>
</setting>
</doc>
^^^看起来很好(我认为)
<html>
<head>
<body>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",'status.php?id=poll',false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
//alert(responseXML);
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("setting");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("status")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
</body>
</html>
^^^只是不确定,Javascript是我的弱点
同样,问题出在我认为的Javascript中,错误显示在Chrome的开发者视图中。