问题
您好,我有一个使用 Jquery POST 和 PHP 的简单搜索系统。搜索后会回显如下信息:
while($row = $search->fetch(PDO::FETCH_ASSOC)) {
echo
'
<br /><br />
'.$row['result_name'].' <br /> Data: <a href="#" id="hover">Click to see</a><br /><br />
';
}
我想这样做,当您将 ID“悬停”(搜索后)悬停时,java 脚本将用数据填充一些 div,它将发送另一个 POST 以获取数据。
这就是我所做的:
对于悬停:
$("#hover").hover(function() {
$.post("ajax_search.php?type=sidebar", { sidebar : seach.val() }, function(get_data) {
stop_load();
$("#sidebar").html(get_data);
});
});
PHP:
if (isset($_GET['type']) && $_GET['type'] == 'sidebar' && isset($_POST['sidebar']))
{
$data = htmlentities(trim($_POST['search']));
$similar = "'%$data%'";
$search = $connect->prepare("SELECT * FROM results WHERE result_name LIKE $similar");
$search->execute();
while($row = $search->fetch(PDO::FETCH_ASSOC)) {
echo
'
<br /><br />
'.$row['result_name'].' <br /> Data: '.$row['rsult_data'].'<br /><br />
';
}
}
但是悬停时什么也没有发生!
问题
我做错了什么?为什么它在悬停时不做任何事情?我可以使用脚本运行后回显的 ID 吗?
额外的
完整的jQuery代码:
$(document).ready(function() {
var search = $("#search");
$("#bla").click(function() {
if (search.val() != '') {
load();
$.post("ajax_search.php", { search : search.val()}, function(data) {
stop_load();
$(".result").html(data);
});
}
else
{
$(".result").html('');
}
$("#hover").hover(function() {
$.post("ajax_search.php?type=sidebar", { sidebar : seach.val() }, function(get_data) {
stop_load();
$("#sidebar").html(get_data);
});
});
function load() {
$('.load').html('<img src="http://i.imgur.com/ZoI4x5I.gif" />');
}
function stop_load(){
$('.load').html('');
}
});
完整的 PHP 代码:
if (isset($_POST['search']))
{
$data = htmlentities(trim($_POST['search']));
$similar = "'%$data%'";
$search = $connect->prepare("SELECT * FROM results WHERE result_name LIKE $similar");
$search->execute();
if ($search->rowCount() == 0) {
echo 'You searched for <b>'.$data.'</b>, 0 results found.';
} else {
$results = $search->rowCount();
echo 'You searched for <b>'.$data.'</b>, '.$results.' results found.';
while($row = $search->fetch(PDO::FETCH_ASSOC)) {
echo
'
<br /><br />
'.$row['result_name'].' <br /> Data: <a href="#" id="hover">Click to see</a><br /><br />
';
}
}
}
if (isset($_GET['type']) && $_GET['type'] == 'sidebar' && isset($_POST['sidebar']))
{
$data = htmlentities(trim($_POST['search']));
$similar = "'%$data%'";
$search = $connect->prepare("SELECT * FROM results WHERE result_name LIKE $similar");
$search->execute();
while($row = $search->fetch(PDO::FETCH_ASSOC)) {
echo
'
<br /><br />
'.$row['result_name'].' <br /> Data: '.$row['rsult_data'].'<br /><br />
';
}
}
谢谢!