-1

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

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I added alert(xmlHttp.responseText); to the script but nothing come out
But, if I open the inspect element with Firebug in FireFox and open the request "GET" under Response I can see it prints out the script
@alkhader: Then it MUST alert your response when you use alert(xmlHttp.responseText);.
Yes Got it. It alerts the scripts now,
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?

1 Comment

Thank you, the text example just to make my post easy. Actually I need to write a complex script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.