I'm trying to implement a dynamic search function in html, jQuery, ajax and php. The problem is that the search function only does the search when you have entered 2-3 letters. E.g: When pressing letter "f" it should return every row in database which contains the letter "f". The return data is empty.(It should return 20 values, i've tested the query) Right now it only returns a result when writing "foo", maybe it's because the that is the only row which have the name. This time it returns the xml structure:
我正在尝试在html,jQuery,ajax和php中实现动态搜索功能。问题是搜索功能仅在您输入2-3个字母时才进行搜索。例如:当按下字母“f”时,它应该返回包含字母“f”的数据库中的每一行。返回数据为空。(它应该返回20个值,我已经测试了查询)现在它只在写“foo”时返回结果,也许是因为那是唯一具有名称的行。这次它返回xml结构:
<picture>
<picuser>CorrcetUser</picuser>
<picurl>CorrectURL</picurl>
<pictime>CorrectTime</pictime>
<picid>CorrectID</picid>
<comment>
<commenttime>Foo</commenttime>
<commentuser>Foo</commentuser>
<commenttext>Foo</commenttext>
</comment>
<description>
Foo bar
</description>
</picture>
Here is the code right now:
这是现在的代码:
the javascript file
javascript文件
$(document).ready(function() {
$(".search").keyup(function() {
var searchbox = $(this).val(); // get the string in the searchbox
var dataString = 'searchword=' + searchbox;
if (searchbox == '') {
} else {
$.ajax({
type : "POST",
url : "search.php",
data : dataString,
dataType : "xml",
async: false,
cache : false,
success : function(data, textStatus, jqXHR) {
//Data repsonse from server
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown)
{
}
});
}
});
});
Here is the search.php
这是search.php
<?php
include ('dbconnect.inc.php');
if ($_POST) {
echo '<?xml version="1.0" standalone="no"?>';
$searchword = $_POST['searchword'];
$sql = "SELECT * FROM picture WHERE description LIKE :searchword";
$query = $dbh -> prepare($sql);
$query -> execute(array('searchword' => '%' . $searchword . '%'));
$nr=0;
$outputXML ="";
$results = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $row) {
$outputUser = $row['userName'];
$outputPicURL = $row['picURL'];
$outputTime = date('Y-m-d H:i', $row['time']);
$outputString = $row['description'];
$outputPicID = $row['pictureID'];
$outputXML.= "<picture>
<picuser>$outputUser</picuser>
<picurl>$outputPicURL</picurl>
<pictime>$outputTime</pictime>
<picid>$outputPicID</picid>
<comment>
<commenttime>Foo</commenttime>
<commentuser>Foo</commentuser>
<commenttext>Bar</commenttext>
</comment>
<description>
Foo bar
</description>
</picture>";
}
echo $outputXML;
}
?>
I thought this was an easy fix, but here I'm stuck. And wondering why data is empty?
我认为这是一个简单的解决方法,但在这里我被卡住了。并想知道为什么数据是空的?
1 个解决方案
#1
0
Xml documents need a single root element. You need to output it:
Xml文档需要一个根元素。你需要输出它:
if ($_POST) {
header('Content-type: application/xml');
echo '<?xml version="1.0" standalone="no"?>';
echo '<pictures>';
...
foreach($results as $row) {
$outputUser = htmlspecialchars($row['userName']);
...
}
echo '</pictures>';
}
Some Browsers (like IE) will only parse the response into a DOM if an XML content type is provided (header('Content-type: application/xml');
).
如果提供了XML内容类型(标题('Content-type:application / xml');),某些浏览器(如IE)将仅将响应解析为DOM。
If you output values in xml using string function (like echo
) you need to escape the values (htmlspecialchars()
). Otherwise characters like <
or &
can break your xml.
如果使用字符串函数(如echo)输出xml中的值,则需要转义值(htmlspecialchars())。否则像 <或&这样的字符可能会破坏你的xml。< p>
Last, if you get XML as a string in Javascript, you can create a DOMParser
and use it to parse it.
最后,如果您在Javascript中将XML作为字符串获取,则可以创建DOMParser并使用它来解析它。
#1
0
Xml documents need a single root element. You need to output it:
Xml文档需要一个根元素。你需要输出它:
if ($_POST) {
header('Content-type: application/xml');
echo '<?xml version="1.0" standalone="no"?>';
echo '<pictures>';
...
foreach($results as $row) {
$outputUser = htmlspecialchars($row['userName']);
...
}
echo '</pictures>';
}
Some Browsers (like IE) will only parse the response into a DOM if an XML content type is provided (header('Content-type: application/xml');
).
如果提供了XML内容类型(标题('Content-type:application / xml');),某些浏览器(如IE)将仅将响应解析为DOM。
If you output values in xml using string function (like echo
) you need to escape the values (htmlspecialchars()
). Otherwise characters like <
or &
can break your xml.
如果使用字符串函数(如echo)输出xml中的值,则需要转义值(htmlspecialchars())。否则像 <或&这样的字符可能会破坏你的xml。< p>
Last, if you get XML as a string in Javascript, you can create a DOMParser
and use it to parse it.
最后,如果您在Javascript中将XML作为字符串获取,则可以创建DOMParser并使用它来解析它。