2015-08-28 106 views
0

我正在实施一个不定式滚动网页。它运作良好。它有如何获取ajax请求到同一页面?

1. index.php

2. getrecords.php两页。

index.php页面

<html> 

//some html codes here 

//my java script 
<script type="text/javascript"> 
    var busy = false; 
    var limit = 6 
    var offset = 0; 
    var anotherID = 5 


    function displayRecords(lim, off) { 
    $.ajax({ 
     type: "GET", 
     async: false, 
     url: "getrecords.php", 
     data: "limit=" + lim + "&offset="+ off+"&anotherID="+anotherID, 
     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 btn-block" type="button">No more records.</button>').show() 
     } else { 
      $("#loader_message").html('<button class="btn btn-default btn-block" type="button"><div id="loader_image"><img src="loader.gif" alt="" width="24" height="24">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> 

</html> 

getrecords.php页面

<?php 
require_once("config.php"); 


$limit = (intval($_GET['limit']) != 0) ? $_GET['limit'] : 10; 
$offset = (intval($_GET['offset']) != 0) ? $_GET['offset'] : 0; 
$id = $_GET["anotherID"]; 
$query = $id; 
$sql = "SELECT * FROM x where title like '%xx%' ORDER BY rand() LIMIT  $limit OFFSET $offset"; 

try { 
$stmt = $DB->prepare($sql); 
$stmt->execute(); 
$results = $stmt->fetchAll(); 
} catch (Exception $ex) { 
echo $ex->getMessage(); 
} 
if (count($results) > 0) { 
foreach ($results as $res) { 
echo 'something'; 
} 
} 
?> 

出于某种原因,我想两个页面合并成一个页面。我想将getrecords.php的编码放入index.php的内部并制作一个页面。我尝试了很多选择,但没有任何帮助。我怎样才能做到这一点?提前致谢。

+1

是什么你遇到的问题? –

+0

当我在我的java脚本中输入index.php而不是getrecords.php时,获取url路径并将整个get records.php代码放在我的index.php中,但它不能帮助我。它会再次回声整个index.php并使其崩溃 – Subi

回答

0

尝试包括getrecords.php内的index.php,写一个条件,以检查是否ajax请求,如果阿贾克斯然后执行登录和退出,否则HTML部分

<?php 

    if(isset($_GET["anotherID"])){ 
    require_once("config.php"); 

    $limit = (intval($_GET['limit']) != 0) ? $_GET['limit'] : 10; 
    $offset = (intval($_GET['offset']) != 0) ? $_GET['offset'] : 0; 
    $id = $_GET["anotherID"]; 
    $query = $id; 
    $sql = "SELECT * FROM x where title like '%xx%' ORDER BY rand() LIMIT  $limit OFFSET $offset"; 

    try { 
    $stmt = $DB->prepare($sql); 
    $stmt->execute(); 
    $results = $stmt->fetchAll(); 
    } catch (Exception $ex) { 
    echo $ex->getMessage(); 
    } 
    if (count($results) > 0) { 
    foreach ($results as $res) { 
    echo 'something'; 
    } 
    } 
    exit; 
    } 
    ?> 

    <html> 

    //some html codes here 

    //my java script 
    <script type="text/javascript"> 
     var busy = false; 
     var limit = 6 
     var offset = 0; 
     var anotherID = 5 


     function displayRecords(lim, off) { 
     $.ajax({ 
      type: "GET", 
      async: false, 
      url: "getrecords.php", 
      data: "limit=" + lim + "&offset="+ off+"&anotherID="+anotherID, 
      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 btn-block" type="button">No more records.</button>').show() 
      } else { 
       $("#loader_message").html('<button class="btn btn-default btn-block" type="button"><div id="loader_image"><img src="loader.gif" alt="" width="24" height="24">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> 

    </html> 
+0

tnk你很多钱..... – Subi

+0

它的100%工作frnd ...但唯一的错误是你仍然设置ajax url路径到'getrecords。 php'当我更改'url:getrecords.php'到'url:index.php'它工作正常... tnk你sooo很frnd ... – Subi

0

当你把ajax放到同一页面时 - 传递一些参数,例如"Getrecords" => TRUE。现在您的整个页面将由两页合并而成,但如果您收到$_POST['Getrecords'] - 然后返回当前位于getrecords页面的脚本的结果,否则在index.php中执行任何您需要的操作。不要忘了更改AJAX参数发布和传递参数

+0

tnk u frnd ...我会尽力... – Subi

0

包裹代码

if (isset($_GET['offset'])) { 
    // All the code from get records.php 
} else { 
    // All the code from index.php 
} 

在第一个页面请求,_GET变量将不会被设置,所以在index.php代码将运行,Ajax请求将在后续请求中提供。

+0

tnk u soo much frnd ... – Subi

+0

tnk u frnd ...它正在工作... – Subi

1

我的问题是:“你为什么要这样做?”。分离顾虑是一件好事。例如,MVC就是基于此。这里有两个功能,一个是页面或视图,一个是服务器操作或控制器,或者可以是REST服务。让他们分开是没有错的。

但我认为你有一个很好的理由。

在你的情况,你想组合成一个“自我指涉”页面2层的功能,要做到这一点,两种方法是最常见的:通过您的AJAX调用

  1. 传递一个特殊的变量并使用if语句。
  2. 通过检测内容类型,自动检测页面是在标准上下文还是AJAX上下文中调用。例如,如果内容类型为json,则返回记录,否则,呈现主视图并执行AJAX调用。例如:$_SERVER["CONTENT_TYPE"] - 但请记住,这不是100%可靠的,您需要确保在您的AJAX请求中传递Content-type标头。
+0

tnk你很多钱... – Subi