我正在用 php 顯示日歷服務器端。現在我想用 2 個按鈕 (<< / >>) 滾動月份而不重新加載整個頁面。有很多問題和例子,我發現了一些非常接近表單的問題,但是,我還沒有真正理解如何將其調整為按鈕。
傳遞請求新資料的一個非常簡單的腳本是
<script>
$(document).ready(function(){
$("moveCal").click(function(){
$("#calendar").load("/presentation/cal.php");
});
});
</script>
php 之前嘗試再請求一個月的嘗試看起來像這樣
echo '<span onclick="updateCal(\''.$toMonth->format('mY').'\')">';
echo '<span class="calendar_month_link"> '.$toMonth->format('M').' ></span></span>';
但是我如何將updateCal()函式與$(document).ready(function(){?
還有一件事讓我有點困惑:如果我不使用存盤在 cookie 中的會話 ID,應用程式服務器仍然只提供具有請求日期的請求 IP,對嗎?其他同時用戶可以選擇其他日期顯示嗎?
uj5u.com熱心網友回復:
你可能想要更多的細節,但這里是它的要點:
用戶訪問一個網頁,地址指向一個 PHP 檔案——該 PHP 檔案在服務器上運行。它每個請求運行一次,因此輸出將只提供給請求者。如果檔案是靜態的,或者它總是產生相同的 HTML 輸出 - 每個人都一樣。
在您的情況下,聽起來您至少需要一個month變數,也許也是一個year變數。當請求帶有變數(如$('#calendar').load('/presentation/cal.php?month=10&year=2021'))時,您可以使用變數值來更改輸出。由于變數來自請求,輸出將特定于傳遞的變數 - 因此站點上的每個用戶都可以發送不同的請求,并且所有人都看到唯一的輸出。
在上面的例子中,月份和年份是通過 GET 傳遞的(即它們被附加到 URL,而不是在表單中或通過 POST AJAX 傳遞,如$.post('cal.php', {month: 10, year: 2021}, function(responseData) { /*...*/ });)。
PHP 依次使用$_GET和$_POST陣列來保存分別通過 GET 和 POST 方法傳遞的引數。因此,在請求的 PHP 檔案 (cal.php) 中,您可以使用$_GET['month']和$_GET['year']訪問請求的月份和年份。您需要檢查它們是否存在于請求中,如果不存在,請使用默認值。如果它們在請求中,您需要驗證它們是否是合法值,如果不是,則使用默認值(或向用戶發送錯誤訊息)。
所以偽代碼可能類似于:
php檔案
<?php
$defaultMonth = date("n");
$defaultYear = date("Y");
$month = isset($_GET['month']) ? $_GET['month'] : $defaultMonth;
/* check that $month is 1 - 12 [or whatever format you're expecting] and if not, set it to $defaultMonth instead, same thing for year */
$year = isset($_GET['year']) ? $_GET['year'] : $defaultYear;
/* query your database for events occurring in month $month and year $year */
/* echo out your calendar HTML with the included events from the requested month/year */
?>
索引.php:
<div id='calendar'></div>
<script src='/path/to/jquery.js'></script>
<script>
var defaultMonth = <?php echo date("n") ?>;
var defaultYear = <?php echo date("Y") ?>;
var currentMonth = defaultMonth;
var currentYear = defaultYear;
function UpdateCal() {
$('#calendar').load('cal.php?month=' currentMonth '&year=' currentYear);
}
function NextMonth() {
currentMonth = 1;
if(currentMonth == 13) {
currentMonth = 1;
currentYear = 1;
}
UpdateCal();
}
function PrevMonth() {
currentMonth -= 1;
if(currentMonth == 0) {
currentMonth = 12;
currentYear -= 1;
}
UpdateCal();
}
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/330634.html
上一篇:無法識別"https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-f
