jQuery Ajax Calendar每隔一个月跳过一次

时间:2018-08-31 21:00:36

标签: php jquery ajax calendar

我一直在为这个ajax日历进行此项目的工作,直到最近为止,它似乎一直运行良好。我显然做了一些事情弄乱了。似乎正在发生的事情是我的请求传递了适当的月份“计数”,但每隔一个请求返回相同的数据。 (我使用了一个子字符串使控制台可读)。在下面的图片中,您可以看到html组合中的月份名称,以及它们如何在重复一个月之前重复两次(例外是12月-> 1月),我的问题是:如何阻止它每隔一个月正确地跳过经过几个月?

repeating months

这使我认为它可能不是我的JS代码,而是我的php代码(说实话,这是一团糟,我嵌套了if-conditionals)

$('#nextmonth').click(function(e){
    e.preventDefault();
    $.ajax({ 
        url: "calendarajax.php?month="+monthcount, 
        type: "POST", 
        dataType: "html",
        success: function(data) {
            var div = $('#ajaxfunc', $(data));
            $('#ajaxfunc').html(data);
            monthcount = monthcount+1;
            monthcountp = -monthcount+2;
            console.log("calendarajaxback.php?month="+monthcountp);
            console.log("------------------------------------------");
            var data = data.toString();
            console.log(data.substring(0,100));
            console.log("------------------------------------------");
        },
        error: function(xhr, ajaxOptions, thrownError){
          //what to do in error
          console.log("couldn't complete request: calendarajaxback.php?month="+monthcountp);
       },
       timeout : 15000//timeout of the ajax call
    });
}),
$('#previousmonth').click(function(e){
    e.preventDefault();
    $.ajax({ 
        url: "calendarajaxback.php?month="+monthcountp, 
        type: "POST", 
        dataType: "html",
        success: function(data) {
            var div = $('#ajaxfunc', $(data));
            $('#ajaxfunc').html(data);
            monthcountp = monthcountp+1;
            monthcount = -monthcountp+2;
            console.log("calendarajaxback.php?month="+monthcountp);
            console.log("------------------------------------------");
            var data = data.toString();
            console.log(data.substring(0,100));
            console.log("------------------------------------------");
        },
        error: function(xhr, ajaxOptions, thrownError){
          //what to do in error
          console.log("couldn't complete request: calendarajaxback.php?month="+monthcountp);
       },
       timeout : 15000//timeout of the ajax call
    });
})

这是我处理请求的两个函数(有“上个月”和“下个月”按钮)。

这是我的php代码(特别是上个月)的一部分,位于另一个文件中,等待请求。 session变量为0或1,并且在12月传递到1月时设置为1,反之亦然(这确定我是否将日历年增加1)。是的,这不是处理它的最佳方法。我怀疑这与我的“ $ numMonth = date(” m“,strtotime('+'。$ monthcount。'month'));有关。”但不太确定。由于缺乏对date()的理解,我的$ _GET请求变量为负;功能,并始终如上所示将内容输入为“ +”。有任何想法吗?

下面的代码用于“下个月”按钮

if(isset($_GET['month'])){
$monthcount = $_GET['month'];
$time = time();
$numDay = date('d', $time);
$numMonth = date("m", strtotime('+'.$monthcount.' month'));
$strMonth = date('F', $time);
$numYear = date('Y', $time);
$firstDay = mktime(0,0,0,$numMonth,1,$numYear);
$daysInMonth = date("t", mktime(0,0,0,$numMonth,1,$numYear));
$dayOfWeek = date('w', $firstDay);
$monthName = date('F', mktime(0, 0, 0, $numMonth, 10));
if($numMonth == 1 || $_SESSION['yeartrack'] == 1){

        $_SESSION['yeartrack'] = 1;
        $monthcount = $_GET['month'];
        $time = time()+60*60*24*365.2425;
        $numMonth = date("m", strtotime('+'.$monthcount.' month'));
        $strMonth = date('F', $time);
        $numYear = date('Y', $time);
        $firstDay = mktime(0,0,0,$numMonth,1,$numYear);
        $daysInMonth = date("t", mktime(0,0,0,$numMonth,1,$numYear));
        $dayOfWeek = date('w', $firstDay);
        $monthName = date('F', mktime(0, 0, 0, $numMonth, 10));
    }   
}else{
    $time = time();
    $numDay = date('d', $time);
    $numMonth = date('m', $time);
    $strMonth = date('F', $time);
    $numYear = date('Y', $time);
    $firstDay = mktime(0,0,0,$numMonth,1,$numYear);
    $daysInMonth = cal_days_in_month(0, $numMonth, $numYear);
    $dayOfWeek = date('w', $firstDay);
    $monthName = date('F', mktime(0, 0, 0, $numMonth, 10));
}

这是项目的图像,只是为了了解我在做什么。 calendar overview

0 个答案:

没有答案