php - 通过 jquery 传递搜索参数

标签 php jquery pdo parameter-passing

我有一个表单,如果用户输入搜索查询,它的参数应该通过 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/

相关文章:

php - 带有 Rewrite 的 ErrorDocument 并且仍然获得Referer

javascript - HTML5-canvas 使圆圈在 Canvas 内闪烁

jquery - html2canvas 不渲染 CDN 图片

PHP PDO 在搜索中未找到结果时显示消息

php - 使用 pdo/php 在表中插入数据

php - 我的 css 使用 codeIgniter 没有正确显示

php - Wordpress - 将 mysql 表导出到带有列标题的 csv

javascript - 格式化 json 以满足我的需要或更好的方法

Jquery 可排序 ('serialize' )

php - Bigquery 是否支持 PHP PDO