Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AJAX从MySQL获取信息时不会返回信息_Javascript_Php_Mysql_Ajax - Fatal编程技术网

Javascript 使用AJAX从MySQL获取信息时不会返回信息

Javascript 使用AJAX从MySQL获取信息时不会返回信息,javascript,php,mysql,ajax,Javascript,Php,Mysql,Ajax,我正在学习W3学校的教程。虽然大部分工作正常,但它似乎没有返回我创建的MySQL数据库中的任何实际信息,exercisedb数据库中的exercises 下面是index.php的代码 <head> <title> Database Fetch Demo </title> <script> function showExercise(str) { if (str == "") { do

我正在学习W3学校的教程。虽然大部分工作正常,但它似乎没有返回我创建的MySQL数据库中的任何实际信息,
exercisedb
数据库中的
exercises

下面是
index.php的代码

    <head>
    <title>
        Database Fetch Demo
    </title>
<script>
function showExercise(str) {
    if (str == "") {
        document.getElementById("txtPlaceholder").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("txtPlaceholder").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getexercise.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form>
    <select name="exercises" onchange="showExercise(this.value)">
        <option value="">Choose an Exercise</option>
        <option value="1">Bench Press</option>
        <option value="2">Deadlift</option>
        <option value="3">Barbell Squat</option>
    </select>
</form>
<br>
<div id="txtPlaceholder">
    <b>No exercise selected.</b>
</div>
<body>
</html>
<!DOCTYPE html>
<html>
<head>
    <style>
    table {
        width: 100%;
        border-collapse: collapse;
    }

    table td, td{
        border: 1px solid black;
        padding: 5px;
    }

    th {text-align: left};
    </style>
</head>
<body>
<?php
$q = intval($_GET['q']);

$connect = mysqli_connect('localhost','root','root','exercisedb');
if(!$connect){
    die('Could not connect ' . mysqli_error($connect));
}

mysqli_select_db($connect, "exercisedb");
$sql = "SELECT * FROM exercises WHERE id = '".q."'";
$result = mysqli_query($connect, $sql);

echo "<table>
<tr>
<th>Exercise Name</th>
<th>Difficulty</th>
<th>Target Areas</th>
<th>Description</th>
</tr>";

while($row = mysqli_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['difficulty'] . "</td>";
    echo "<td>" . $row['targetareas'] . "</td>";
    echo "<td>" . $row['description'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($connect);
?>
</body>
</html>
你不应该发邮件

<!DOCTYPE html>
<head>
<body>


在ajax中,只回答所需div的内容。

在JS、DB或服务器上是否生成了任何错误消息?您从AJAX调用中得到了什么?您完成了哪些基本调试?你检查过你的浏览器控制台了吗?您是否直接转到
getexercise.php?q=1
检查您的php代码是否有效?你知道这是javascript还是php问题吗?你在
$sql=“SELECT*FROM exercises WHERE id=”.q.“”中有
q
而不是
$q
欢迎来到StackOverflow!为了提高可读性,我改进了问题的标题和措辞。请添加@wolffer east提到的您收到的任何错误检查您的JavaScript、数据库和服务器是否有错误。这并不能真正回答问题,但确实是OP应该知道的。