我有一个表单,如果用户输入搜索查询,它的参数应该通过 jquery 传递,并且在获得结果后应该将结果加载到 div 容器中。由于我不太精通 jquery,我该怎么做?
html:
//currently the data is being displayed on pageload:
$(document).ready(function() {
$("#bquote").load("quotes_in.php")
});
$(".bsearch")
.keydown(function() {
//pass the parameter to the php page
//display in div #bquote
});
<!-- Begin Search -->
<div id="search" style="display:none;">
<input type="text" class="bsearch" name="search" value="Search" />
</div>
<!-- End Search -->
php [使用 OOP]:
if (isset($_POST["search"]))
{
$quotes->searchQuotes();
}
PHP 类:
$search = $_POST['search'];
$sql = "SELECT * FROM thquotes WHERE cQuotes LIKE '%"
. mysql_real_escape_string($search) ."%' ORDER BY idQuotes DESC";
try {
$query = $this->_db->prepare($sql);
$query->execute();
if(!$query->rowCount()==0)
{
while($row = $query->fetch())
{
echo $this->formatSearch($row);
}
最佳答案
我会这样做:
$(".bsearch").keydown(function() {
//create post data
var postData = {
"bsearch" : $(this).val(),
"anotherParam" : "anotherValue"
};
//make the call
$.ajax({
type: "POST",
url: "yourAjaxUrl.php",
data: postData, //send it along with your call
success: function(response){
alert(response); //see what comes out
//fill your div with the response
$("#bquote").html(response);
}
});
});
编辑:
要放置加载程序,您需要在此处检查:
http://api.jquery.com/category/ajax/global-ajax-event-handlers/
并显示加载器图像,例如:
$("#loading").ajaxStart(function(){
$(this).show();
});
并在 ajax 调用完成时隐藏它:
$("#loading").ajaxComplete(function(){
$(this).hide();
});
关于php - 通过 jquery 传递搜索参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3339731/