如何使用ajax调用名称onkey?

时间:2016-07-28 10:53:13

标签: php json ajax ajax4jsf

我是ajax的新手。我想根据onkey up从数据库中获取名称,但我在控制台中收到此错误“在分配中无效的左侧”。当我键入其显示“showame函数未定义”时。     这是我的代码。          搜索:          

    

This is myscript
function showname(d) {
        var xhttp;
        if (window.XMLHttpRequest) {
            xhttp= new XMLHttpRequest();
        }else{
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
        document.getElementById("demo").innerHTML = xhttp.responseText;
           }
          };
          xhttp.open("POST", "ajax2.php?name"=+d, true);
          xhttp.send();
    }
this ajax2.php code

$name = $_GET["name"];
$db = new mysqli("localhost", "root", "", "customername");
$conn = $db->query("select customerName from customers like '".$name."%'");
$fetch = $conn->fetch_array();

?>
<?php foreach ($fetch as $key => $value) :?>
    <p><?php echo $value ?></p>
<?php endforeach; ?>

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

xhttp.open(“POST”,“ajax2.php?name”= + d,true);

我认为你有一个拼写错误,把=插入引号。

xhttp.open("POST", "ajax2.php?name=" + d, true);