dongpigui8898 2016-11-26 06:42
浏览 128
已采纳

使用AJAX时没有得到任何响应

This is my html page

<html>
<head>
    <title>Using AJAX</title>
</head>
    <script type="text/javascript" src="ajax.js"></script>
<body>
    <form action="searchItems.php" name="searchItem" method="post">
        Enter item name : <input type = "text" name = "itemName" id = "itemName" placeholder = "Enter item name" onblur="ajax()" />
        <input type = "submit" name="submit" id="submit" value="Search" />
        <input type = "reset" name = "reset" id= "submit" value = "Reset" />

        <div id="desc"></div>
    </form>
</body>

This is the php page when the form is submitted. The results are as expected.

<?php
include('dbconnect.php');
if (isset($_POST["submit"])) {
    //echo "<pre>".print_r($_POST,1)."</pre>";
    $conn   = db_connect();
    $name   = $_POST['itemName'];
    $query  = "SELECT * FROM itemreceived where name = '" . $name . "'";
    $result = $conn->query($query);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_object()) {
            echo $row->description;
        }
    } else {
        echo "No result";
    }
}
?>

This is the dbconnect.php page used in the searchItems.php page

<?php
function db_connect()
{
    $conn       = null;
    $servername = "localhost";
    $username   = "root";
    $pwd        = "";
    $db_name    = "ebay_db";
    $conn       = mysqli_connect( $servername, $username, $pwd, $db_name );
    if ( !$conn ) {
        die( "Connection failed :" . $conn->connect_error );
    }
    If ( $conn ) {
        //echo '<script type="text/javascript"> alert("Connection successful"); </script>';
    }
    return $conn;
}
?>

This is the ajax() function used in the html page to get the result in the div "desc".

function ajax(){
var itemName = document.getElementById('itemName').value;
console.log(itemName);
var url="searchItems.php";
envelop = new XMLHttpRequest();
envelop.open("POST",url,true);
envelop.onreadystatechange=displayResult;
envelop.setRequestHeader("Content-type","application/x-www-form-urlencoded");
envelop.send('itemName='+itemName); 
alert("Calling ajax");

}

function displayResult(){
    if((envelop.readyState==4) &&( envelop.status==200)){
        document.getElementById('desc').innerHTML=envelop.responseText;
        alert("Part 2");
    }
}

When I submit the form, I get the expected result in the page searchItems.php The ajax() function is not working. The objective is to get the result in div "desc" as soon as the function blur() is activated.

  • 写回答

3条回答 默认 最新

  • duanlv2788 2016-11-26 08:15
    关注

    remove isset($_POST["submit"]) because its just only for submiting form and it returns fals for your ajax function!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)