我正在尝试使用Jquery + PHP在MongoDB中实现Ajax实时数据搜索。
所以我遵循this tutorial并尝试对其进行转换,使其可与MongoDB一起使用。
index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Live Data Search using Jquery PHP MONGODB</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<br />
<br />
<br />
<h2 align="center">Ajax Live Data Search using Jquery PHP MONGODB</h2><br />
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Search</span>
<input type="text" name="search_text" id="search_text" placeholder="Search by Customer Details" class="form-control" />
</div>
</div>
<br />
<div id="result"></div>
</div>
<div style="clear:both"></div>
<br />
<br />
<br />
<br />
</body>
</html>
<script>
$(document).ready(function(){
load_data();
function load_data(query)
{
$.ajax({
url:"fetch.php",
method:"post",
data:{query:query},
success:function(data)
{
$('#result').html(data);
}
});
}
$('#search_text').keyup(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
fetch.php
<?php
require 'vendor/autoload.php';
$output = '';
if(isset($_POST["query"]))
{
$search = $_POST["query"];
echo $search;
}
else
{
// the array of product criteria
$client = new MongoDB\client;
$userDB = $client->AW;
$profilecollection = $userDB->DA1;
$query = $profilecollection->find(array(),array("full_name"=>1));
foreach($query as $result)
{
//echo $result['Author'];
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>Customer Name</th>
</tr>
<tr>
<td>'.$result['full_name'];'</td>
</tr>';
}
echo $output;
}
?>
但是它没有按预期工作。当我使用var_dump($result['full_name']);
时
它会转储数据库中的所有数据,当我搜索某些内容时,它什么也不会显示...