duanfu7994 2015-07-19 09:26
浏览 40

如何在分页中使用ajax获取数据?

I want to get 3 data from a page using ajax get method.

The javascript is

<script type="text/javascript">
  var busy = false;
  var limit = 15
  var offset = 0;
  var id = 150;

  function displayRecords(lim, off) {
    $.ajax({
      type: "GET",
      async: false,
      url: "s.php",
      data: "limit=" + lim + "&offset=" + off,
      cache: false,
      beforeSend: function() {
        $("#loader_message").html("").hide();
        $('#loader_image').show();
      },
      success: function(html) {
        $("#results").append(html);
        $('#loader_image').hide();
        if (html == "") {
          $("#loader_message").html('<button class="btn btn-default"    type="button">No more records.</button>').show()
        } else {
          $("#loader_message").html('<button class="btn btn-default"  type="button">Loading please wait...</button>').show();
        }
        window.busy = false;


      }
    });
  }

  $(document).ready(function() {
    // start to load the first set of data
    if (busy == false) {
      busy = true;
      // start to load the first set of data
      displayRecords(limit, offset);
    }

    $(window).scroll(function() {
      // make sure u give the container id of the data to be loaded in.
      if ($(window).scrollTop() + $(window).height() >  $("#results").height() && !busy) {
         busy = true;
        offset = limit + offset;
        // this is optional just to delay the loading of data
        setTimeout(function() { displayRecords(limit, offset); }, 500);
        // you can remove the above code and can use directly this function
        // displayRecords(limit, offset);
      }
    });

  });

 </script>

and s.php is

$limit = (intval($_GET['limit']) != 0 ) ? $_GET['limit'] : 10;
$offset = (intval($_GET['offset']) != 0 ) ? $_GET['offset'] : 0;
$id = $_GET['id'];
echo $id;

but i can't get the value of id, each time i'm trying new way something went wrong or it shows error, the php file doesnot get the id value using ajax. but it 'll gets the values of limit and offset perfectly. both limits and offsets are not a constant values but the id is constant one.

How can I fix this..?

  • 写回答

3条回答 默认 最新

  • doubi8383 2015-07-19 09:30
    关注

    You can't get the value of $id because it not being sent in the ajax request.

    data: "limit=" + lim + "&offset=" + off + "&id=" + id,
    

    You might wanna add id to the data string. It should work now.

    评论

报告相同问题?