为什么这个ajax调用不读我的内部php?

时间:2010-02-20 19:52:37

标签: php ajax

要跟踪的编辑太多,但我已经简化了问题。我在index.php中有这段代码:

<div class="calendar_top">
<?php
include(SITE_ROOT . "/includes/sub_top_divs.php"); ?>
</div>
<table class="tablebodycontainer"><tr><td>

<?php

    include(SITE_ROOT . "/includes/view-monthly-calendar-ajax.php");

?>
</td></tr></table>

<?php include(SITE_ROOT . "/includes/copyright.php"); ?>
</div>

上面的子话题包含导航链接:

包括/ subtop-divs.php:

<?php
    //include(SITE_ROOT . "/includes/set-variables.php");//dateFormat($date)
    echo "<table class='navtabs' cellpadding='0' cellspacing='0'>";
    echo "<tr><td class='right'>";
    echo buildMenuNavigation($currentPageIndex);
    echo '</td></tr></table>';
?>

这是buildMenuNavigation函数:

function buildMenuNavigation($currentIndex=0) {

  $navtabs = array(
  '0'=>array('Monthly'=>'index.php'),
  '1'=>array('Daily'=>'agenda.php'),
  '2'=>array('Admin'=>'admin/view-timelines.php'),
  '3'=>array('Help'=>'help.php'),
  );

  $sep = '<li>&nbsp;|&nbsp;</li>';$builtNav=array();
  foreach($navtabs as $index=>$tablinks) {  
    foreach($tablinks as $key=>$value)  {
      $class='';
      if($index==$currentIndex) {
        $class=' class="selected"';
      }
      //pr($value);
      $builtNav[] = '<li><a href="' . SITE_URL . '/' . $value.'"' . $class .'>&nbsp;'.$key.'&nbsp;</a></li>';
    }
  }
    return '<ul>' . implode($sep,$builtNav) . '</ul>';
}

切换页面时实际更改的唯一数据是上面的内容:

<?php

    include(SITE_ROOT . "/includes/view-monthly-calendar-ajax.php");

?>

所以这似乎是ajax的完美候选人。

以下是/includes/view-monthly-calendar-ajax.php的内容:

<?php
$counter = 0; 
?>
<table class="tabbody">
  <tr>
    <td class='head unselectable'>Sun</td>
    <td class='head unselectable'>Mon</td>
    <td class='head unselectable'>Tue</td>
    <td class='head unselectable'>Wed</td>
    <td class='head unselectable'>Thu</td>
    <td class='head unselectable'>Fri</td>
    <td class='head unselectable'>Sat</td>
  </tr>
  <tr>
    <?php
    //echo $year."-".$month."-";
    $flag       =   0;
    $daysInrow  =   0;
    for($i = 1; $i < $numDays+1; $i++, $counter++)
    {
      $daysInrow++;
      $zero = "";
      if($i < 10)
      {
        $zero = "0";
      }

      $t_date  = $year."-".$month."-".$zero.$i;
      //$t_date         =   "$year-$month-$i";
      $timeStamp        =   strtotime($t_date);
      $eventID      =   0;
      $eventName        =   "&nbsp;";
      $bgColor      =   "";
      $funcBG           =   "setbgcolorMonth($i);";

      if($i == 1)
      {
        // Workout when the first day of the month is
        $firstDay = date("w", $timeStamp);

        for($j = 0; $j < $firstDay; $j++, $counter++)
        echo "<td class='tddaybox'></td>";
      }

      if($counter % 7 == 0)
      {
        $daysInrow = 1;
        echo "</tr><tr>";
      }

      if(date("w", $timeStamp) == 0)
        if($i == date("d") && $month == date("m") && $year == date("Y"))
              $class = "class='today'";
          else
            $class = "class='weekend'";
      else
      if($i == date("d") && $month == date("m") && $year == date("Y"))
      $class = "class='today'";
      else
      $class = "class='normal'";

     $numric_time =  getNumericTime($_SESSION['userData']['timezone']);//Get the numeric timezone

      $query            =   "SELECT *  FROM events  WHERE date(convert_tz(StartDate,'+00:00','". $numric_time."'))='".$t_date."' AND UserID='" . $_SESSION['userData']['UserID'] ."' ORDER BY PTLType ASC";
        //br();
      $result           =   mysql_query($query);
      if(mysql_num_rows($result))//cursor:hand;cursor:pointer;width:112px;
      {
        $funcBG     =   "";
        echo "<td valign='top' class='a_cursor_width tddaybox' id='day$i' onclick='setbgcolorMonth($i)'>";
        echo "<div class='td_overlow'>";
        echo '<table style="width:100%;border:0;">';
        echo '<tr class="rowColor"><td '.$class.'><div class="div_left"><a href="'.SITE_URL.'/agenda.php?date='.$year.'-'.$month.'-'.$zero.$i.'">'.$i.'</a></div><div class="div_left_80"  onclick="'.$funcBG.' get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID='.$eventID.'\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\');return false">&nbsp;</div></td></tr>';

        while($row = mysql_fetch_assoc($result))
        {
          $eventID  =   $row['EventID'];
          $parentEventID    =   $row['ParentEventID'];
          $eventName    =   stripslashes($row['EventName']);
          $PTLType  =   $row['PTLType'];
          $textclass    =   "title4";

          $onclick_call = 'get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID='.$eventID.'\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\')';

          if($PTLType   != 0)  {
            $onclick_call = 'get_event_popup(window.event, this, \''.SITE_URL.'/timeline-popup.php\', \'eventID='.$eventID.'\', \'parentEventID='.$parentEventID.'\', \'type=M\')';
            $bgColor    =   "";
            $textclass  =   "redtext";
            if($PTLType == 3)  {
              $textclass     =  "bluetext";
              $display  =   "none";
            }  else  {
              $display  =   "block";
            }
            $event_name_wrapped = '<div>'.$eventName.'</div>';
          }  else  {
            $textclass      =   "mainEvent";
            $display    =   "block";
            $event_name_wrapped = '<div>'.$eventName.'</div>';
          }

          echo '<tr><td onclick="'.$onclick_call.';return false;" class="'.$textclass.' a_cursor" title="'.$eventName.'" style="display:'.$display.';">'.$event_name_wrapped.'</td></tr>';
        }
        echo '<tr><td onclick="get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID=0\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\');return false;" class="a_cursor">&nbsp;</td></tr>';
        echo "</table>";
        echo "</div>";
        echo "</td>";
      }
      else
      {
        echo '<td id="day'.$i.'" height="80" valign="top" onclick="'.$funcBG.' get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID='.$eventID.'\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\');return false;" class="tddaybox a_cursor">';
        echo '<table style="width:100%;">';
        echo "<tr class='rowColor'><td $class>$i</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "</table>";
        echo "</td>";
      }
    }

    for($l=0;$l<7-$daysInrow;$l++)
    {
      echo "<td class='tddaybox'>&nbsp;</td>";
    }
    ?>
  </tr>
</table>
<?php
/*
if(isset($divRed))
{
  $divRed   =   implode(",",$divRed);
  echo "<div id='divRed1' style='display:none'>$divRed</div>";
}
else
{
  echo "<div id='divRed1' style='display:none'>0</div>";
}
if(isset($divBlue))
{
  $divBlue= implode(",",$divBlue);
  echo "<div id='divBlue1' style='display:none'>$divBlue</div>";
}
else
{
  echo "<div id='divBlue1' style='display:none'>0</div>";
}
if(isset($divMainEvent))
{
  $divMainEvent=    implode(",",$divMainEvent);
  echo "<div id='divMainEvent1' style='display:none'>$divMainEvent</div>";
}
else
{
  echo "<div id='divMainEvent1' style='display:none'>0</div>";
}
*/
?> 

<div id='gig1' style='display:none'>0</div>
<div id='todo1' style='display:none'>0</div>
<div id='completed1' style='display:none'>1</div>


<input type="hidden" id='lastselectedday' value='' />

所以上面所有的输出现在都是正确的,但是当我尝试这样做时:

<form>
<input type="button" value="Display Message" onclick="getData('/Dev2/includes/view-monthly-calendar-ajax.php', 'targetDiv')">
</form>
<table class="tablebodycontainer">
<tr>
<td id="MainCalendarContainer">
<div class="ajaxswap" id="targetDiv">

</div>

ajax调用的输出不执行php。

4 个答案:

答案 0 :(得分:1)

首先,你不应该通过Ajax拉<script>个元素,它们不会被解释,如果它们是,它们将在第二个请求中破坏所有内容。

首先在浏览器中调用/Dev2/includes/view-monthly-calendar-ajax.php进行一些调试。查看您获得的源代码。 PHP在文档交付之前运行,因此无法提取HTML,但 PHP生成的内容。他们是一个实体。

您的PHP代码中有错误导致输出错误,或者您的PHP代码根本没有被解释,这很容易被发现,因为原始PHP源代码将在文档的源代码中(绝不可能永远发生。)

答案 1 :(得分:0)

PHP代码由服务器处理;除非网络服务器出现问题,否则浏览器永远不会看到它。

答案 2 :(得分:0)

在将数据发送到客户端之前预处理PHP。服务器解析并执行php代码,只发送代码输出的内容。如果你想获得完整的文件,你需要让它不被php处理,例如通过将文件扩展名重命名为其他东西。

答案 3 :(得分:0)

你应该实现另一个php页面,例如......

 getData('getPHP.php?page=/Dev2/includes/view-monthly-calendar-ajax.php', 'targetdic');

然后在该页面中,您需要实现一个方法来将php文件作为字符串加载并返回它。目前正在按照您的预期解释php。