搜索并加载结果jquery ajax

时间:2013-08-18 06:43:58

标签: php jquery ajax search

我是AJAX / jQuery的新手,我只是在探索一些东西 这是我的问题我有一个文本框,我想要做的是如果我点击搜索按钮,搜索结果将加载(#sorting-results)我不知道这有什么问题...我希望有人能让我了解更多。感谢。

的index.php

<input type="text" name="item_type" class="item_type"></td>
        <select class="location">
          <option value="bldg1">bldg1</option>
          <option value="bldg2">bldg2</option>
        </select>
        <button class="btn_search">Search</button>

      <div id="sort-results"></div>

的script.js

$(".btn_search").click(function(){
  type = $(".item_type").val();
  location = $(".location").val();
    if(type.length > 0){
    $.ajax({
      type: "GET",
      url: "sort.php",
      data: "type="+type +"location="+location,
      cache:false,
      success : function(){
        $("#sort-results").html();
      }

    });
   }

});

Sort.php

require 'global/db.php';
require_once 'config.php';
$type = $_GET['type'];
$location = $_GET['location'];

$db = new Database(DBHOST, DBUSER, DBPASS, DB);
$selection = $db->select("SELECT * FROM os_inventory WHERE os_type like '$type' AND os_location like '$location'");
echo "<table class=bordered>
        <tr><th>Item</th>
        <th>Item Code</th>
        <th>Item Type</th>
        <th>Serial No.</th>
        <th>Date Added</th>
        </tr>";
foreach ($selection as $key) {
    echo "<tr>";
    echo "<td>" . $key['os_id'] . "</td>";
    echo "<td>" . $key['os_type'] . "</td>";
    echo "<td>" . $key['os_code'] . "</td>";
    echo "<td>" . $key['os_serial'] . "</td>";
    echo "<td>" . $key['os_date'] . "</td>";
    echo "</tr>";
}
echo "</table>";

2 个答案:

答案 0 :(得分:1)

您是否忘记实际传递内容?

success : function(html){
             $("#sort-results").html(html);
          }

答案 1 :(得分:0)

也许我错了,但我还没有看到jQuery选择器使用句号。将您的选择器更改为:

$("#btn_search").click(function(){
      type = $("#item_type").val();
      location = $("#location").val();

然后根据@ David的帖子来实际接收来自服务器的代码以放入DIV