我正在使用xmlhttp请求将数据发送到我的php文件,然后使用它从数据库中获取数据,然后在我的原始页面显示该响应。以下是代码:
<script>
function func2(str) {
if (str == "") {
document.getElementById("replaceContent").innerHTML = "";
return;
} else {
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) {
document.getElementById("replaceContent").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","projectdata.php?q="+str,true);
xmlhttp.send();
}
}
&#13;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="replaceContent">
This should be replaced.
<button onclick="func2(10)"> Click ME </button>
</div>
</body>
</html>
&#13;
以下是projectData.php:
<?php
include 'connEst.php';
// get the q parameter from URL
$q = $_GET['id'];
echo $q;
$sql="SELECT * FROM projects WHERE ID = '".$q."'";
$result = mysqli_query($conn,$sql);
echo "<table>
<tr>
<th> $q </th>
</tr>
<tr>
<th>Project Name</th>
<th>Project Location</th>
<th>Project Value</th>
<th>Client Name</th>
<th>Work Done in Project</th>
<th>Project Summary</th>
<th>Start Date</th>
<th>End Date</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['p_name'] . "</td>";
echo "<td>" . $row['p_loc'] . "</td>";
echo "<td>" . $row['p_val'] . "</td>";
echo "<td>" . $row['client_name'] . "</td>";
echo "<td>" . $row['p_wdone'] . "</td>";
echo "<td>" . $row['p_summary'] . "</td>";
echo "<td>" . $row['start_date'] . "</td>";
echo "<td>" . $row['end_date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
&#13;
我使用apache服务器安装在我的mac上运行它。我正在Chrome中对此进行测试。 我设置的$ q变量在运行文件时返回一个空值。
请帮忙!
答案 0 :(得分:0)
您的Ajax函数发送参数&#34; q&#34;不是&#34; id&#34;并且PHP查找$_GET['id']
在projectData.php中,将现有内容更改为:
$q = $_GET['q'];
或者将您的javascript更改为:
xmlhttp.open("GET","projectdata.php?id="+str,true);