2013-03-29 49 views
0

我想创建一个函数,我的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文件。

+4

请问您能告诉我们您正在接收哪个错误? – Rikesh

+1

你在哪里得到未定义的索引错误。这似乎是PHP错误,对不对? – Neo

+0

我得到我的PHP文件calendar_start.php我宣布 $ showmonth = $ _POST ['showmonth']; $ showyear = $ _POST ['showyear']; –

回答

0

地方

hr.send(vars); // Actually execute the request 之后hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

手段

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 
// Send the data to PHP now... and wait for response to update the status div 
hr.send(vars); // Actually execute the request 

hr.onreadystatechange = function() { 
(hr.readyState == 4 && hr.status == 200) { 
var return_data = hr.responseText; 
document.getElementById("showCalendar").innerHTML = return_data; 
} 
} 
document.getElementById("showCalendar").innerHTML = "processing..."; 
} 
+0

我很抱歉,但似乎没有工作。我可否知道这样做的理由? –

+0

,因为它似乎是你收到你的回应后发送数据 – mukund

+0

我认为这是因为我激活它与 –

0

Replce

showmonth = month; 
showyear = year; 

威特h

var showmonth = month; 
var showyear = year; 
+0

我很抱歉,但似乎并没有工作=( –