此查询和功能有什么问题?

时间:2019-07-18 19:04:22

标签: php ajax

我尝试解决此代码。但是不幸的是,ajax无法正常工作。我还没有找出错误。请帮助我。

  

index.html

<form>
<select name="languages" onchange="showLanguage(this.value)">
    <option value="">Select Programming Language</option>
    <option value="1">Python</option>
    <option value="2">Java</option>
    <option value="3">C++</option>
 </select>
</form>
<br>
<div id="languageDetails">Programming Related Info</div>

<script>
function showLanguage(string) 
{
    if (string == "") 
    {
        document.getElementById("languageDetails").innerHTML = "";
        return;
    } 
    else 
    {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() 
        {
            if (request.readyState == 4 && request.status == 200)
            {
               document.getElementById("languageDetails").innerHTML = 
               request.responseText;
            }
        };
         request.open("GET","languageDetails.php?i="+string,true); 
         request.send();
    }
}
</script>
  

languageDetails.php

<?php
$i=$_GET["i"];

$conn = mysqli_connect('localhost','root',' ','db');

if (!$conn) 
{
    die('Not connect ' . mysqli_error($conn));
}

$sql="SELECT * FROM language WHERE id = '".$i."'";
$result = mysqli_query($conn,$sql);

echo "<table>
<tr>
<th>Id</th>
<th>Programming Name</th>
<th>Born</th>
</tr>";
while($row = mysqli_fetch_array($result)) 
{
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['born'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($conn);
?>
  

code.sql

CREATE DATABASE db;

CREATE TABLE IF NOT EXISTS language 
(
  id int(3) NOT NULL AUTO_INCREMENT,
  name varchar(50) NOT NULL,
  born int(4) NOT NULL,
  PRIMARY KEY (id)
)ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

INSERT INTO language(id, name, born) VALUES
(1, 'Python', 2005),
(2, 'Java', 1995),
(3, 'C++', 1986);

0 个答案:

没有答案