2013-09-28 119 views
0

誰能告訴我爲什麼我的AJAX POST是不工作...我加alert()和它的工作... IDK的哪裏是錯的... ...這是我的代碼...爲什麼AJAX POST不起作用?

<script language="JavaScript" type="text/javascript"> 
    function initialCalendar() { 
     var hr = new XMLHttpRequest(); 
     var url = "calendar.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.."; 
     alert(vars); 
    } 

    function next_month(){ 
     var nextmonth = showmonth + 1; 
     if(nextmonth > 12) { 
      nextmonth = 1; 
      showyear = showyear + 1; 
     } 
     showmonth = nextmonth; 
     var hr = new XMLHttpRequest(); 
     var url = "calendar.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").innertHTML = return_data; 
      } 
     } 
     hr.send(vars); 
     document.getElementById("showCalendar").innertHTML = "processing..."; 
     alert(vars); 
    } 

    function last_month(){ 
     var lastmonth = showmonth - 1; 
     if(lastmonth < 1) { 
      lastmonth = 1; 
      showyear = showyear - 1; 
     } 
     showmonth = lastmonth; 
     var hr = new XMLHttpRequest(); 
     var url = "calendar.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").innertHTML = return_data; 
      } 
     } 
     hr.send(vars); 
     document.getElementById("showCalendar").innertHTML = "processing..."; 
     alert(vars); 
    } 
</script> 

而且身體

<body onLoad="initialCalendar();"> 
<div id="showCalendar"> </div> 
</body> 

,我用下一個和以前的按鈕...... 這是calendar.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 class="title_bar">'; 
echo '<div class="previous_month"><input name="myBtn" type="submit" value="Previous Month" onClick="javascript:last_month();"></div>'; 
echo '<div class="show_month">' . $showmonth . '/' . $showyear . '</div>'; 
echo '<div class="next_month"><input name="myBtn2" type="submit" value="Next Month" onClick="javascript:next_month();"></div>'; 
echo '</div>'; 
+1

是不是一個jQuery選項? – Jonast92

+0

@ Jonast92沒有jQuery – JeraldPunx

+0

試着用這個代替'documment.getElementById()':'var doc = document;函數E(e){return doc.getElementById(e); }',並保存一些輸入。 – PHPglue

回答

1

更改innertHTML,至innerHTML您的next_month()last_month()功能。我建議使用像Komodo Edit 7.1這樣的編輯器。 (我認爲這是Windows的最新免費版本,不知道關於Mac)

+0

omg ty太多了... btw我用記事本++哈哈..我會用 – JeraldPunx