使用php和ajax在html中显示表格


displaying table in html using php and ajax

我是AJAX的新手,必须从PHP中显示html表。当我按下生成按钮时,它应该显示下表。这是我在HTML文件中的代码:

function showHint()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    //what codes to put here when in terms of button.
    }
  }
xmlhttp.open("GET","list.php", true);
xmlhttp.send();
}
</script>
</head>
<body>
    <button onClick="showHint()">Generate</button>
</body>

xmlhttp.onreadystatechange=function()部分,应该是什么陈述?

这是我的PHP文件:

<?php
$a[]="James Burb";
$a[]="Melvin Chapman";
$a[]="Eric Chan";
$a[]="Aira Gomez";
echo "<table border=1>";
for($i=0; $i<count($a); $i++)
{
echo "<tr>";
echo "<td>" .$hint=$a[$i]. "</td>";
echo "</tr>";
}
echo "</table>";
?>

提前谢谢。

您可以使用xmlhttp.responseText在某些div中显示您的表,如下所示:

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }
}

它将把list.php返回的表放在div中。