我想創建一個函數,我的php日曆可以更新。這組代碼應該向控制器發送$ _POST [「showmonth」]和$ _POST [「showyear」],但顯然它不工作,導致未定義的索引錯誤。說實話,我是一個JavaScript的總新手,只是按照YouTube上的教程。下個月和上個月有兩種類似的功能可以通過onClick觸發器激活。任何人都有什麼建議可以解決問題?未定義的索引javascript php錯誤
我收到錯誤在第9行和我的PHP文件,該文件在那裏我宣佈
$showmonth = $_POST['showmonth']; and $showyear = $_POST['showyear'];
通知10:未定義指數:showmonth在/ home/jharvard /虛擬主機/本地主機/ HTML/calendar_start。 PHP的第9行
<script language="JavaScript" type="text/javascript">
function intialCalendar(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
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);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("showCalendar").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById("showCalendar").innerHTML = "processing...";
}
</script>
這是我的html文件
<!DOCTYPE html>
<html>
<head>
<link href="css/bootstrap.css" rel="stylesheet"/>
<link href="css/bootstrap-responsive.css" rel="stylesheet"/>
<link href="css/styles.css" rel="stylesheet"/>
<link href="css/calcss.css" rel="stylesheet" type= "text/css" media = "all"/>
<?php if (isset($title)): ?>
<title>Calendar: <?= htmlspecialchars($title) ?></title>
<?php else: ?>
<title>Calendar</title>
<?php endif ?>
<script src="js/jquery-1.8.2.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/scripts.js"></script>
<script src="js/calendar_functions.js"></script>
</head>
<body onload="initialCalendar();">
<div class="container-fluid">
<div id="top">
<a href="calendar_start.php"><img alt="Calendar" src="img/square.png"/></a>
</div>
<div id="middle">
<div id="showCalendar"></div>
這是我的PHP文件
<?php
// configuration
require("../includes/config.php");
require("../templates/show_calendar.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(mktime(0, 0, 0, $showmonth, $day_count, $showyear))));
echo '<div id="calendar_wrap">';
echo '<div class="previous_month"><input name="myBtn" type="submit" value="Previous Month" onclick="javascript:last_month();"></div>';
echo '<div class="next_month"><input name="myBtn" type="submit" value="Next Month" onclick="javascript:next_month();"></div>';
echo '<div class="title_bar">';
echo '<div class="showmonth">' . $showmonth . '/' . $showyear . '</div>';
echo '</div>';
echo '<div class="week_days">';
echo '<div class="days_of_week">Sun</div>';
echo '<div class="days_of_week">Mon</div>';
echo '<div class="days_of_week">Tue</div>';
echo '<div class="days_of_week">Wed</div>';
echo '<div class="days_of_week">Thur</div>';
echo '<div class="days_of_week">Fri</div>';
echo '<div class="days_of_week">Sat</div>';
echo '<div class="clear"></div>';
echo '</div>';
//previous month filler days
if($pre_days != 0)
{
for($i=1; $i<=$pre_days; $i++)
{
echo '<div class="non_cal_day"></div>';
}
}
//current month
for($i=1; $i<=$day_count; $i++)
{
echo '<div class="cal_day">';
echo '<div class="day_heading">'.$i.'</div>';
echo '</div>';
}
//next month filler days
if($post_days != 0)
{
for($i=1; $i<=$post_days; $i++)
{
echo '<div class="non_cal_day"></div>';
}
}
echo '</div>';
require("../templates/footer.php");
?>
誤差的另一個原因,我懷疑可能是使用的需要()我的HTML與控制器(calendar_start.php)聯繫起來,因爲它是與別人教我做什麼的視頻教程,唯一的區別。該視頻是使用Dreamweaver,而我使用gedit和不使用require()我不知道如何將PHP文件鏈接到HTML文件。
請問您能告訴我們您正在接收哪個錯誤? – Rikesh
你在哪裏得到未定義的索引錯誤。這似乎是PHP錯誤,對不對? – Neo
我得到我的PHP文件calendar_start.php我宣佈 $ showmonth = $ _POST ['showmonth']; $ showyear = $ _POST ['showyear']; –