weixin_33743248 2015-04-05 16:11 采纳率: 0%
浏览 2

Javascript Ajax请求

Basically what I'm trying to do is have a text field where the user enters in a foodstuff, and as theyre typing a message updates to say whether or not said item is in stock (through contacting the database).

My php code for the xml:

require 'connect.php'; 
header('Content-Type: text/xml');
echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>";

echo '<response>';
if (isset($_GET['foodInput'])&&!empty($_GET['foodInput'])) {
    extract($_GET);
    $sql = "SELECT * FROM food WHERE name=:foodInput";
    $stmt = $db -> prepare($sql);
    if ($stmt ->execute(array(':foodInput'=>$foodInput))) {
        if ($stmt -> rowCount()>0) {
            $results = $stmt -> fetch(PDO::FETCH_OBJ);
            echo 'Yes, we do in fact have '.$results->name.'.';     
        }else{ echo 'No, '.$foodInput.' is not available champ.'; }
    }
}else{ echo 'Please enter foodstuff.'; }

echo '</response>';

My Javascript for the rest:

function createHttp(){
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    };
    return xmlhttp;
}

var xmlhttp = createHttp();

function handleServerResponse(){
    if(xmlhttp.readyState==4){
        if (xmlhttp.status==200) {
            xmlResponse = xmlhttp.responseXML;
            xmlDoc = xmlResponse.documentElement;
            message = xmlDoc.firstChild.data;
            document.getElementById('updated').innerHTML = message;
            setTimeout(process, 1000);
        };
    }else{
        alert('Something is amiss');
    }
}

function process(){
    if (xmlhttp.readyState==4||xmlhttp.readyState==0){
        food = encodeURIComponent(document.getElementById('food').value);
        xmlhttp.open('GET', 'include/foodstore.php?foodInput='+food,true);
        xmlhttp.onreadystatechange = handleServerResponse;
        xmlhttp.send();
    }else{
        setTimeout(process, 1000);
    }
}

As soon as the process is initialised I get the alert something is amiss message that occurs when readyState is no longer 4. I entered a console.log(xmlhttp.readyState) and the resulting log was basically 1234123412341234 etc. So instead of sticking it out at 4 it would do something, else? Honestly I'm not particularly sure what readyState is.

When I remove this alert and I'm allowed to enter text it updates as the purpose requires. But as soon as the alert is turned back on, bingo there it is again.

Any suggestions? Really new to this area.

Thanks

Edit: Html

<form action="">
        <label for="food">Enter foodstuff:</label>
        <input type="text" id="food" name="foodInput" onkeyup="process()"><br>
    </form>
    <h4 id="updated"></h5>
  • 写回答

2条回答 默认 最新

  • weixin_33699914 2015-04-05 16:20
    关注

    You run your process function only on DOM load but you should call it when the value of the input is changed with onkeyup="process()".

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)