我想用 php 創建一個可以在服務器端滾動的日歷。昨天我收到了關于如何使用 Ajax 實作這一點的好建議(我的嘗試失敗了)。雖然答案看起來非常有希望和正確,但我沒有讓它起作用。可能它就像一條錯誤的路徑一樣簡單,但是我在路徑上的嘗試沒有奏效。不幸的是,我不知道如何除錯它。
問題是,單擊按鈕更新資料(滾動日歷)不會提供任何反應。但是,會呈現初始日歷視圖。
檔案結構:
index.php
booking1.php
presentation (folder)
booking2.php
cal.php
home.php
Safari 中顯示的客戶端代碼(在上半部分的末尾,您將找到用于日歷滾動的按鈕和相應腳本的按鈕)
<span id="calendar">
<section class="container">
<div class="row">
<div class="col">
<div class="card">
<div class="card_body">
<div class="calendar_month">
<span class="calendar_month_title">
<button onclick="prevMonth()">< Sep</button>
Oct
</span>
// ....
</section>
<script scr="assets/js/bootstrap.bundle.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
var defaultMonth = 10;
var defaultYear = 2021;
var currentMonth = defaultMonth;
var currentYear = defaultYear;
function updateCal() {
$('#calendar').load('presentation/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>
</body>
這是presentation/cal.php檔案
<?php
$defaultMonth = date("m");
$defaultYear = date("Y");
$month = isset($_GET['month']) ? $_GET['month'] : $defaultMonth;
if (strlen($month) === 1 ) {
$month = '$month' . strval($month);
}
$year = isset($_GET['year']) ? $_GET['year'] : $defaultYear;
$requestedDate = new DateTime(strval($year) . '-' . strval($month) . '-01');
echo '<section >';
echo '<div >';
echo '<div class = "col">';
echo '<div class = "card"><div >';
getCalendar($requestedDate, true, true, false);
echo '</div></div>';
echo '</div>';
echo '</section>';
在booking1.php從主目錄(calendar.php主機getCalendar()功能)
<?php
define('HOME_DIR',__DIR__);
require_once __DIR__.'/calendar/calendar.php';
include __DIR__.'/presentation/header.php';
include __DIR__.'/presentation/booking.php';
和 presentation/booking2.php
<section class="container" id="tbd">
<?php
echo '<span id="calendar">';
include __DIR__.'/cal.php';
echo '</span>';
?>
</section>
<script scr="assets/js/bootstrap.bundle.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.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('presentation/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>
</body>
知道為什么單擊按鈕沒有反應嗎?我將如何除錯它?這實際上只是一個小型的私人專案,我還沒有設法讓 Eclipse 與 MySQL 一起作業,以至于我可以使用 Eclipse 進行除錯。這就是為什么我出于除錯原因列印變數等。但是,如果頁面已成功呈現并且僅更新部分失敗,則此方法不再有效(在我的(非)專業水平上)
uj5u.com熱心網友回復:
您應該使用瀏覽器進行除錯。在 Chrome/Firefox(未使用 Safari)中,右鍵單擊并檢查。然后Console選項卡將顯示任何 javascript 錯誤,您也可以console.log()從代碼中除錯任何變數,Network選項卡將向您顯示請求,因此您可以查看 ajax 請求是否正在執行,是否拋出錯誤,或者什么是發送的資料和接收的資料。
如果可能的話,我還建議從頭開始重寫代碼并改進架構。
還:
- 從
presentation/booking2.php,您正在執行.load('presentation/cal.php')但不確定是否需要將目錄放在這里,因為您已經在其中,因為它可能會嘗試轉到presentation/presentation/cal.php,但同樣,您將在控制臺和網路中的瀏覽器開發人員工具中看到這一點 - 不要使用a
span,而是adiv,對于容器,最好使用html標簽 - 確保你只從一個地方生成日歷,這樣你就不會重復代碼并以奇怪的東西結束,我的意思是,如果你有一個函式可以回傳你的日歷代碼,你應該在第一次使用這個函式在渲染 html 時,以及在 ajax 呼叫中,或者更好的是,總是通過 ajax 加載日歷
- 我建議從頭開始撰寫一個簡單的頁面,它的行為與您希望的一樣,您可以在其中正確實作從 ajax 加載塊的效果,并使用按鈕“導航”它,然后當它起作用時,您可以將其余代碼遷移到它并逐步測驗以防萬一發生故障,以便您知道何時/什么原因導致它并能夠回傳并糾正
最初通過 ajax 加載日歷的示例
檢查https://learn.jquery.com/using-jquery-core/document-ready/
注意:您使用的是相當舊版本的 jQuery,但無論如何該示例應該可以作業。
在<script>標簽的末尾,添加:
<script>
//...
$( document ).ready(function() {
console.log( "loading initial calendar" );
updateCal();
});
//...
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/326289.html
上一篇:ajaxjson表分組行
