- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我开始学习 Ajax,我做了这个显示用户输入的简单 HTML 页面,当他使用 ajax 输入书名(存储在 php 文件中的数组中)时,用户可以在输入下方看到他打字时的结果,这是我无法做到的部分,这是代码:
<!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>
这是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!");
}
}
}
这是 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>';
?>
我无法显示 JS 文件应该做什么,有人知道如何解决吗?
编辑:我将所有文件放在 Wamp 下的 www 文件夹中。
最佳答案
您发布的内容有一些错误。
首先,你有一个错字:
echo '<?xml version="1.0" enconding="UTF-8" standalone="yes" ?>';
^ the "n"
应该读作编码
。
将错误设置为在您的服务器上显示会抛出以下内容:
XML Parsing Error: XML declaration not well-formed Location: http://www.example.com/book.php?book.php?book= Line Number 1, Column 21:
另外,请记住,正如我在评论中所述,Book1
和 book1
的处理方式不同,因此数组键被视为大小写-敏感的。
在 Stack 上查阅以下答案:
您还在为 while 使用单个等号进行赋值:
elseif ($book='')
^
而不是比较,它应该包含一个额外的等号:
elseif ($book=='')
^^
引用资料:
另外,确保 PHP 确实已安装、运行并正确配置。
关于javascript - 显示书名的简单ajax脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32343310/
Book_title Table +---------------+-------------------+ | isbn | title | +------
如何删除 PHP 中的所有 HTML 标记,除了 <>人物? //There's other HTML tags, like h1, div, etc. echo strip_tags(' a hot
我是一名优秀的程序员,十分优秀!