通过jquery传递搜索参数
我有一个表单,如果用户输入搜索查询,其参数应通过 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);
}
i have a form in which if the user enters the search query, its parameter should be passed through jquery and after getting the results it should load the results in the div container. since i'm not very well-versed with jquery, how would i do this?
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 [using OOP]:
if (isset($_POST["search"]))
{
$quotes->searchQuotes();
}
php class:
$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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会这样做:
编辑:
要放置加载程序,您需要在此处检查:
http://api.jquery.com/category/ajax/global-ajax-event-handlers/
并显示加载程序图像,例如:
并隐藏当 ajax 调用完成时:
I would do in that way:
EDIT:
For putting a loader you need to check this here:
http://api.jquery.com/category/ajax/global-ajax-event-handlers/
And to show a loader image for example:
And to hide it when ajax call is complete:
如果您想走 ajax 路线...
请阅读 jQuery.ajax() 如果您将该搜索框转换为正确的形式,请使用 jQuery.serialize()
If you want to go down the ajax route...
Read up on jQuery.ajax() and if you turn that search box into a proper form, utilize jQuery.serialize()
您可以使用 $_GET 或 $_REQUEST 代替 $_POST,如下所示:
然后,在 PHP 中,将
... 替换为
Instead of using $_POST, you can use $_GET or $_REQUEST like the following:
Then, in PHP, replace
...with