我正在尝试通过AJAX和PHP进行sql数据库调用,其结果将显示在网页中。 javascript似乎工作正常,并且正在正确将GET请求传递到PHP页面,并且未生成任何错误消息。但是,PHP仅返回部分代码。这显示在网页上,代替了我的数据库内容:
0){ while($row = mysqli_fetch_assoc($result)) { echo "
"; echo $row['home']; echo " vs. "; echo $row['away']; echo "
我的第一个怀疑是PHP中的语法错误,但是经过几次尝试后,我找不到一个(作为新手也无济于事!)。
.js函数:
function calldropdown(gameweek) {
var gameweek = document.getElementById('gameweek').value
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("matches").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","/static/gameweek.php?q="+gameweek,true);
xmlhttp.send();
}
.php文件(已编辑):
<?php
$q = intval($_GET['q']);
$servername = "myservername";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";
$con = mysqli_connect($servername, $username, $password, $dbname);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"mydbname");
$sql="SELECT home, away FROM matches WHERE gameweek = '".$q."'";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)) {
echo "<p>";
echo $row['home'];
echo " vs. ";
echo $row['away'];
echo "</p>";
}
} else {
echo "error";
}
mysqli_close($con);
?>
有什么想法吗?