简单的ajax脚本,用于显示书籍的名称

Had*_*adh 3 html javascript php ajax

我开始学习Ajax并且我做了这个显示用户输入的简单HTML页面,当他输入一本书的名称(存储在php文件中的数组中)时,使用ajax,用户可以在下面看到输入结果,而他类型,这是我无法做到的部分,这是代码:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="bookstore.js"></script>
  <link rel="stylesheet" href="style.css">
</head>
<body onload="process()">
  <h1>
    Hadhemi's BookStore !
  </h1>
  Enter the book you want to order
  <input type="text" id="userInput">
  <div id="underInput"> </div>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是JS文件

// 3 functions : create an object, communicate and response
var xmlHttp=createXmlHttpRequestObject();

function createXmlHttpRequestObject() 
{
    var xmlHttp; 

    if(window.ActiveXObject)
    {
        try 
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //check for IE
        }
        catch (e)
        {
            xmlHttp = false;
        } 
    }
    else 
    {
        try 
        {
            xmlHttp = new XMLHttpRequest(); // ! IE
        }
        catch (e)
        {
            xmlHttp = false;
        }
    }

    if (!xmlHttp) 
        alert("Can't Create that object");
    else
        return xmlHttp;
}

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

function handleServerResponse() 
{
    //sends back an xml file

    if (xmlHttp.readyState==4) 
    {
        if(xmlHttp.status==200) 
        {
            xmlResponse = xmlHttp.responseXML;
            xmlDocumentElement=xmlResponse.documentElement;
            message = xmlDocumentElement.firstChild.data; 
            document.getElementById("underInput").innerHTML= message;
            setTimeout('process()',1000); 
        }

        else
        {
            alert("OOps! Something went wrong!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是PHP文件:

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

echo'<response>';

$book = $_GET['book'];
$bookArray = array('Book1','Book2','Book3');
if(in_array($book, $bookArray))
    echo 'We do have'.$book.'!';
elseif ($book='') 
    echo 'Enter a book name idiot!';
else
    echo 'We dont have'.$book.'!';

echo'</response>';

?>
Run Code Online (Sandbox Code Playgroud)

我无法显示JS文件应该做什么,有谁知道如何修复它?

编辑:我把所有文件放在Wamp下的www文件夹中.

Fun*_*ner 6

你发布的内容有些不对劲.

首先,你有一个拼写错误:

echo '<?xml version="1.0" enconding="UTF-8" standalone="yes" ?>';
                              ^ the "n"
Run Code Online (Sandbox Code Playgroud)

应该读作哪个encoding.

将错误设置为在您的服务器上显示会抛出以下内容:

XML解析错误:XML声明格式不正确位置:http://www.example.com/book.php? book.php?book =第1行,第21列:

另外,请记住,正如我在评论中所述,Book1并且book1没有对待它们,因此数组键被视为区分大小写.

请在Stack上查看以下答案:

您还在使用单个等号进行分配:

elseif ($book='')
             ^
Run Code Online (Sandbox Code Playgroud)

而不是比较,它应该包含一个额外的等号:

elseif ($book=='')
             ^^
Run Code Online (Sandbox Code Playgroud)

参考文献:

另外,确保PHP确实已安装,运行并正确配置.