PHP没有加载和意图提示没有发生

时间:2014-03-19 19:32:18

标签: javascript php html

我按照教程试用了服务器和网页之间的接口。当我将所有3个文件html,js,php放在我的桌面上时,它不起作用。我知道php是服务器端脚本,因此将3个文件放在web主机文件夹public_html上。当我运行它时,它也不起作用。请告诉我我做错了什么。我认为代码是正确的,因为它来自教程。我把文件放在错误的地方吗?谢谢你的帮助。

注意:当html加载并且我在文本框中输入信息时,下面应该有一个提示,告诉我输入的项目是否存在。在我的情况下,它提示什么都不起作用。

HTML

<!DOCTYPE HTML>
<html>
    <head>
        <script src="foodstore.js"></script>
    </head>
    <body onload="process()">
        <h3>The Chuff Bucket</h3>
        <p>Enter the food you want to enter:</p>
        <input type="text" id="userInput" />
        <div id="underInput" />
    </body>
</html>

JS

var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject(){
    var xmlHttp;

    if (window.ActiveXObject) {
        //in case user using IE
        try{
            xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
        }
        catch(e){
            xmlHttp = false; 
        }
    }
    //firefox, chrome
    else{
        try{
            xmlHttp = new XMLHttpRequest(); 
        }
        catch(e){
            xmlHttp = false; 
        }
    }

    if (!xmlHttp) {
        alert("Can't create object"); 
    }
    else{
        return xmlHttp; 
    }


function process(){
    if (xmlHttp.readyState==0 || xmlHttp.readyState==4 ) {
        food = encodeURIComponent(document.getElementById("userInput").value);
        xmlHttp.open("GET", "foodstore.php?food=" + food, true); 
        xml.onreadystatechange = handleServerResponse;  
        xmlHttp.send(null); 
    }
    else{
        setTimeOut("process()", 1000);
    }
}

function handleServerResponse(){
    if (xmlHttp.readyState==4) {
        if (xmlHttp.status==200) {
            xmlResponse = xmlHttp.responseXML; 
            xmlDocumentElement = xmlResponse.documentElement; 
            message = xmlDocumentElement.firstChild.data; 
            document.getElementById("underInput").innerHTML = '<span style="color:blue">' + message + '</span>'; 
            setTimeOut("process()", 1000);
        }
        else{
            alert("Something went wrong!"); 
        }
    }
}

PHP

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

    echo "<response>";
        $food = $ GET["food"];
        $foodName = array("tuna", "bacon", "beef", "loaf", "ham";
        if (in_array($food, $foodName) {  
            echo "We do have " . $food. "!";
        }

        elseif (food == "") {
            echo "Please enter the food name.";
        }

        else{
            echo "We do not have " . $food. "!";
        }
    echo "</response>";
?>

0 个答案:

没有答案