0

My index.php

    switch ($action){

         case "calendar":

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

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

         echo "</script>";

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

         break;

    }

my calendar_view.php:

I have an ajax function which should call the action calendar from the index and display the result on calendar_view.php in calendar div

<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>

The calendar div:

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

When I call the calendarList function it did call the case, but did not display the result in calendar div.

Any help. Thanks

alkhader
  • 960
  • 6
  • 16
  • 33
  • You're doing it wrong. Please see http://stackoverflow.com/questions/11658596/is-echoing-javascript-code-condtionally-based-on-server-side-logic-considered-ha/11658717 – Dmitry Pashkevich Aug 13 '12 at 08:33

2 Answers2

1

Instead of using a script to write thee text just output text

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

I don't think inserting scripts with innerHTML works see Can scripts be inserted with innerHTML?

Community
  • 1
  • 1
Musa
  • 96,336
  • 17
  • 118
  • 137
1

There is not much to display there. If everything works like you described, the response is appended to the DOM, but the JavaScript-alert will never be executed.

Try alert(xmlHttp.responseText); to be absolutely sure that your response is correct. If it is, you can go from there.

Amberlamps
  • 39,180
  • 5
  • 43
  • 53