2012-08-13 63 views
0

javascript代碼我的index.php用PHP編寫

switch ($action){ 

     case "calendar": 

     echo "<script language=\"javascript\">\n"; 

     echo "document.write('Hello World!')"; 

     echo "</script>"; 

     include($_STR_TEMPLATEPATH."/calendar_view.php"); 

     break; 

    } 

我calendar_view.php:

我有一個AJAX功能,應該調用從indexactioncalendarcalendar divcalendar_view.php顯示結果

<script> 
    function calendarList() 
    { 
    var xmlHttp; 
    try 
     { 
     // Firefox, Opera 8.0+, Safari 
     xmlHttp=new XMLHttpRequest(); 
     } 
    catch (e) 
     { 
     // Internet Explorer 
     try 
     { 
     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
     } 
     catch (e) 
     { 
     try 
      { 
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
     catch (e) 
      { 
      alert("Your browser does not support AJAX!"); 
      return false; 
      } 
     } 
     } 
    xmlHttp.onreadystatechange=function() 
    { 
    if (xmlHttp.readyState==4 && xmlHttp.status==200) 
     { 
     document.getElementById("calendar").innerHTML=xmlHttp.responseText; 
     } 
    } 

     xmlHttp.open("GET","?action=calendar",true); 

     xmlHttp.send(null); 

    } 
    </scritp> 

日曆格:

<div id="calendar"></div> 

當我打電話給calendarList function它確實調用了這種情況,但沒有在calendar div中顯示結果。

任何幫助。由於

+0

你做錯了。請參閱http://stackoverflow.com/questions/11658596/is-echoing-javascript-code-condtionally-based-on-server-side-logic-considered-ha/11658717 – 2012-08-13 08:33:06

回答

1

沒有太多的顯示在那裏。如果所有內容都按照您所描述的那樣工作,則將響應附加到DOM,但JavaScript警報將永遠不會執行。

嘗試alert(xmlHttp.responseText);確定您的回覆是正確的。如果是這樣,你可以從那裏出發。

+0

我加了alert(xmlHttp.responseText);但沒有出來 – alkhader 2012-08-13 08:39:11

+0

但是,如果我在FireFox中打開Firebug的檢查元素並在響應下打開請求「GET」,我可以看到它打印出腳本 – alkhader 2012-08-13 08:40:22

+1

@alkhader:然後它必須在使用時提醒您的響應'警報(xmlHttp.responseText);'。 – Amberlamps 2012-08-13 08:42:25

1

而不是使用腳本來寫你的文本只輸出文本的

switch ($action){ 
    case "calendar": 
    echo "Hello World!"; 
    include($_STR_TEMPLATEPATH."/calendar_view.php"); 
    break; 
} 

我不認爲與innerHTML的作品中插入腳本看到Can scripts be inserted with innerHTML?

+0

謝謝,文本示例只是爲了讓我的發佈簡單。其實我需要寫一個複雜的腳本。 – alkhader 2012-08-13 08:36:42