在Ajax响应中浏览

时间:2014-09-14 19:44:02

标签: php jquery ajax

我是jquery&的新手阿贾克斯。

我有一个带有jquery的php文件,它在选择日期时从另一个php文件加载内容

    $("#datepicker3").change(function(){
        $.ajax({
                type: "POST",
                url: "ptreport1.php",
                data: 'ptdate=' + $(this).val(),
                success: function(resultt){
                    $('#searchr').html(resultt);
                    $("#searchr").show();
                }
                });
});

我面临的问题是ptreport1.php中的内容包含浏览该php文件范围的链接(分页)。当用户点击这些链接时,他将被引导出原始页面,从中加载ajax内容。 无论如何要防止这种情况并在原始页面中加载内容。

  

更新#2。

使用preventDefault编写代码后,以下是我的jquery

<script>
$(document).ready(function() {    
$("#searchr").hide(); 
$(function() {
$('#datepicker3' ).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd M yy'
});
});
    $("#datepicker3").change(function(){
    var myValue = $(this).val();
        $.ajax({
                type: "POST",
                url: "ptreport1.php",
                data: 'ptdate=' + $(this).val(),
                success: function(resultt){
                    $('#searchr').html(resultt);
                    console.log('html.resultt');
                    $("#searchr").show();
                $('.pagination-link').click(function(e) {
                        e.preventDefault();
                       //Another AJAX call to load the content here.
                       var page = $("#pagediv").html();
                       var timestamp =$("#timediv").html();
                       var dataString = 'page='+ page + '&timestamp='+ timestamp;
                               $.ajax({
                type: "GET",
                url: "ptreport1.php",
                data: dataString,
                success: function(resultt){
                    $('#searchr').html(resultt);
                    console.log('html.resultt');
                    $("#searchr").show();
                }
                });
                    });    
                }

                });
});  
});
</script>

和ptreport1.php

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include "core.php";
connectdb();
$page = $_GET['page'];
$timestamp = $_GET['timestamp'];
$ptdate = $_POST['ptdate'];
if(isset($timestamp)){
$ptdate = date('d M Y', $_GET['timestamp']);
}else{    
$timestamp =  strtotime($ptdate);  
}
 if(!isset($page)){
     $page=1;
 }
    if($page=="" || $page<=0)$page=1;
$numresults = mysqli_query($connect, "SELECT patient.firstname, patient.lastname, appointment.charge FROM patient INNER JOIN appointment ON  patient.id = appointment.patientid WHERE DATE_FORMAT(appointment.date, '%d %b %Y')='".$ptdate."'")or die("error querying");
$numrows = mysqli_num_rows($numresults);
    $num_items = $numrows; //changable
    $items_per_page= 5;
    $num_pages = ceil($num_items/$items_per_page);
    if($page>$num_pages)$page= $num_pages;
    $limit_start = ($page-1)*$items_per_page;    
    $npage = $page+1;
$numresults = mysqli_query($connect, "SELECT patient.firstname, patient.lastname, appointment.charge FROM patient INNER JOIN appointment ON  patient.id = appointment.patientid WHERE DATE_FORMAT(appointment.date, '%d %b %Y')='".$ptdate."' LIMIT $limit_start, $items_per_page")or die("error querying");
$numrows = mysqli_num_rows($numresults);
if($numrows==0){
    echo "Sorry, No results for $ptdate.";
    exit();
}
echo "<p><table>";
while($msdrow = mysqli_fetch_assoc($numresults)){
        $charge= $msdrow['charge'];
        $fname= $msdrow['firstname'];
        $lname= $msdrow['lastname'];
      // $fullname = $fname." ".$lname;
echo "<tr><td>$fname</td><td>$lname</td><td>$charge</td></tr>";
$totalcharge = $totalcharge+$charge;
    }
echo "<b><tr><td colspan=\"2\">Total Charge</td><td>$totalcharge</td></tr></b></table></p>";
echo "<div id='pagediv' >";
echo "$page</div>";
echo "<div id='timediv' >";
echo "$timestamp</div>";  
  echo "<p align=\"center\">";   
    if($page>1)
    {
      $ppage = $page-1;
      echo "<a href=\"ptreport1.php?timestamp=$timestamp&amp;page=$ppage\" class=\"pagination-link\">&#171;PREV</a> ";
    }
    if($page<$num_pages)
    {
      $npage = $page+1;
      echo "<a href=\"ptreport1.php?timestamp=$timestamp&amp;page=$npage\" class=\"pagination-link\">Next&#187;</a>";
    }
    echo "<br/>$page/$num_pages<br/>";
echo "</p>";

我在这里做错了什么,因为它在第二次点击后跳出ajax指向原始的php页面。

感谢名单

1 个答案:

答案 0 :(得分:0)

在加载ptreport1.php中的内容后,您需要在分页链接上设置onclick事件处理程序。您需要更改.pagination-link以匹配您正在加载的内容中的链接。

success: function(resultt){
                    $('#searchr').html(resultt);
                    $("#searchr").show();
                    $('.pagination-link').click(function(e) {
                        e.preventDefault();
                       //Another AJAX call to load the content here.
                    });
                }