Javascript函数没有被执行。为什么?

时间:2018-05-16 21:30:53

标签: javascript php html

我试图在不刷新页面的情况下以html格式打印表格,这一切都基于下拉菜单中的选择。我正在访问我的数据库并正确填充下拉菜单,但是当表格出现时,脚本甚至都没有被执行。

我确定我应该做的更多事情,但我不知道是什么。

main.php

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>


<script language="javascript" type="text/javascript">
function showCustomer(str){
    var choice = str.value;

    var xmlhttp;
    if (choice==0){         
        document.getElementById("display").innerHTML="No person selected";
        return;
    }
    if(window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    }else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function();{
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("display").innerHTML=xmlhttp.responseText;          
        }
    }
    xmlhttp.open("GET","showtable.php?q="+str,true);
    xmlhttp.send();
}
</script>


<p>
<form>
<select name="customer" onchange="showCustomer(this)">
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=lab9",$username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
    $dbh->exec("SET CHARACTER SET UTF8");
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
$stmt=$dbh->prepare("SELECT id,name FROM customers");
//echo "<p>";
//echo "<select name=\"customer\" onchange=\"".<script>showCustomer(this.value)</script>;."\">";
$zero=0;
echo "<option value=".$zero.">Select person</option>";
$stmt->execute();
$result=$stmt->fetchAll();
foreach ($result as $row)
{
      //unset($id, $name);
      $id = $row['id'];
      $name = $row['name']; 
      echo "<option value=".$id.">".$name."</option>";                 
}
?> 
</select>
</form>
</p>


<p id="display"></p>
</body>
</html>

showtable.php

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

$hostname = 'localhost';
$username = 'root';
$password = '';
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=lab9",$username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
    $dbh->exec("SET CHARACTER SET UTF8");
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
$stmt=$dbh->prepare("SELECT id,name,adress,city,tk,phone,mobile FROM customers WHERE id=".$q."");
$stmt->execute();
$result=$stmt->fetchAll();
foreach($result as $row){
    echo "<p>";
    echo "<table border='1'>";
    echo "<tr>";
    echo "<td>ID</td><td>".$row['id']."</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Name</td><td>".$row['name']."</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Adress</td><td>".$row['adress']."</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Town</td><td>".$row['city']."</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Τ.Κ.</td><td>".$row['tk']."</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Telephone number</td>".$row['phone']."<td></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Mobile</td><td>".$row['mobile']."</td>";
    echo "</tr>";
    echo "</table>";
    echo "</p>";
}


?> 

1 个答案:

答案 0 :(得分:0)

正如@Xufox所说,将function();{更改为function() {。同样在 showtable.php ,你的"SELECT id,name,adress,city,tk,phone,mobile FROM customers WHERE id=".$q.""也许你输错了?从地址到地址?如果有帮助,请告诉我!