所以我设置了它,所以当我单击一个按钮时,它会创建一个XMLHttpRequest对象,用于获取我想要的数据。
onclick按钮运行
function allcontactstable() {
var url = "php_scripts/allcontactstable.php";
tablerequest.open("GET", url, true);
tablerequest.onreadystatechange = inserttable;
tablerequest.send(null);
}
php是
$mysql = mysql_connect("localhost", "admin", "");
$db = mysql_select_db("arw29");
$query = "Select `firstname`, `lastname`, `email` from `contacts`";
$result = mysql_query($query);
$output = null;
$output .= '<table>';
while($row = mysql_fetch_assoc($result)){
$output .= '<tr>';
$output .= '<td>'.$row['firstname'].'</td>';
$output .= '<td>'.$row['lastname'].'</td>';
$output .= '<td>'.$row['email'].'</td>';
$output .= '</tr>';
}
$output .= '</table>';
echo $output;
mysql_close();
并且回调函数是
function inserttable() {
if (tablerequest.readyState == 4) {
if (tablerequest.status == 200) {
var response = tablerequest.responseText;
document.getElementById("main").innerHTML = response;
}
else {
alert("Error: " + tablerequest.statusText);
}
}
}
结果是我的网页上的div不包含我期望的格式化表格,而是包含字符串
'
'; while($row = mysql_fetch_assoc($result)){ $output .= ''; $output .= ''.$row['firstname'].''; $output .= ''.$row['lastname'].''; $output .= ''.$row['email'].''; $output .= ''; } $output .= ''; echo $output; mysql_close(); ?>'
尝试搜索,找不到与此相关的任何内容。