簡體   English   中英

帶有Javascript / Ajax的PHP日歷

[英]Php calendar with Javascript/Ajax

我正在創建一個php日歷,並且正在使用Javascript / Ajax,以便在幾個月之間滾動而不必刷新頁面。 該日歷在沒有Javascript / Ajax的情況下工作正常,但現在僅顯示純白色屏幕。

對不起,一大堆代碼

代碼: ** show_calendar.php **

    <html>
    <head>
    <link href="calstyle.css" rel="stylesheet" type="text/css" media="all" />
    <script language="JavaScript" type="text/javascript">
    function initialCalendar(){
        var hr = new XMLHttpRequest();
        var url = "calendar_start.php";
        var currentTime = new date ();
        var month = currentTime.getMonth() + 1;
        var year = currentTime.getFullYear();
        showmonth = month;
        showyear = year;
        var vars= "showmonth="+showmonth+"&showyear="+showyear;
        hr.open("POST", url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if (hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                    document.getElementById("showCalendar").innerHTML = return_data;
            }
        }
        hr.send(vars);
        document.getElementbyId("showCalendar"). innerHTML = "processing...";
    }
    </script>
    <script language="JavaScript" type="text/javascript">
    function next_month() {
        var next_month = showmonth + 1;
        if(nextmonth > 12) {
            nextmonth = 1;
            showyear = showyear+1;
        }
        showmonth = nextmonth;
        var hr = new XMLHttpRequest();
        var url = "calendar_start.php";
        var vars= "showmonth="+showmonth+"&showyear="+showyear;
        hr.open("POST", url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if (hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                    document.getElementById("showCalendar").innerHTML = return_data;
            }
        }
        hr.send(vars);
        document.getElementbyId("showCalendar"). innerHTML = "processing...";
    }
    </script>
    <script language="JavaScript" type="text/javascript">
    function last_month() {
        var last_month = showmonth - 1;
        if(lastmonth <1 ) {
            lasttmonth = 12;
            showyear = showyear-1;
        }
        showmonth = lastmonth;
        var hr = new XMLHttpRequest();
        var url = "calendar_start.php";
        var vars= "showmonth="+showmonth+"&showyear="+showyear;
        hr.open("POST", url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if (hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                    document.getElementById("showCalendar").innerHTML = return_data;
            }
        }
        hr.send(vars);
        document.getElementbyId("showCalendar"). innerHTML = "processing...";
    }
    </script>
    </head>
    <body onload="initialCalendar();">
    <div id="showCalendar"> </div>
    </body>
    </html>

**Calendar_start.php**

<?php
$showmonth = $_POST['showmonth'];
$showyear = $_POST['showyear'];
$showmonth= preg_replace('#[^0-9]#i', '', $showmonth);
$showyear= preg_replace('#[^0-9]#i', '', $showyear);

$day_count = cal_days_in_month(CAL_GREGORIAN, $showmonth, $showyear);
$pre_days = date('w', mktime(0,0,0, $showmonth, 1, $showyear));
$post_days = (6-(date('w', mktime(0,0,0, $showmonth, $day_count, $showyear))));

echo '<div id="calendar_wrap">';
echo '<div class="title_bar">';
echo '<div class="previous_month"><input name="submit" type="submit" value="Previous Month" onClick="javascript:last_month();"></div>';
echo '<div class="show_month">'  . date('F', mktime(0, 0, 0, $showmonth)) . ' ' . $showyear . '</div>';
echo '<div class="next_month"><input name="submit" type="submit" value="Next Month" onClick="javascript:next_month();"></div>';
echo '</div>';

echo '<div class="week_days">';
echo '<div class="days_of_the_week">Sun</div>';
echo '<div class="days_of_the_week">Mon</div>';
echo '<div class="days_of_the_week">Tues</div>';
echo '<div class="days_of_the_week">Wed</div>';
echo '<div class="days_of_the_week">Thur</div>';
echo '<div class="days_of_the_week">Fri</div>';
echo '<div class="days_of_the_week">Sat</div>';
echo '<div class="clear"></div>';
echo '</div>';

if ($pre_days != 0) { 
    for($i=1; $i<=$pre_days; $i++) {
        echo '<div class="non_cal_day"></div>';
    }
}

for ($i=1; $i<= $day_count; $i++) {
    echo '<div class="cal_day">';
    echo '<div class="day_heading">' . $i . '</div>';
    echo '</div>';
}

if ($post_days !=0) {
    for($i=1; $i<=$post_days; $i++) {
        echo '<div class="non_cal_day"></div>';
    }
}
echo '</div>';
?>

讓我們一起去吧。

首先,您已經編寫了三份代碼,您確實應該將它們放入一個函數中,例如, function load_month(n)使用load_mont(current)load_month(current+1)load_month(current-1)調用的function load_month(n) (我留給您在此處執行實際代碼)。

其次,您至少有兩種情況下的錯誤: var currentTime = new date (); 應該具有Date並且在第23行中, getElementbyId缺少大寫getElementbyId B

第三,在我的本地服務器默認的php安裝中,似乎缺少日歷擴展名(至少是cal_days_in_month方法)。 因此,如果在進行了前面的更正后,您的代碼仍然無法正常工作,則可能需要調查一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM