tags:

views:

60

answers:

4

Hi folks,

Too many edits to keep track of, but I have simplified the issue. I have this code in my 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>

The subtop above contains the navigation links:

includes/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>';
?>

Here is that buildMenuNavigation function:

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>';
}

The only data that actually changes when switching pages is the contents in the above:

<?php

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

?>

So this seems like a perfect candidate for ajax.

Here is the contents of /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='' />

So the output for all of the above is correct now, but when I try and do this:

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

The output of the ajax call doesn't execute the php.

A: 

The PHP code is processed by the server; the browser will never see it unless the web server glitches.

Ignacio Vazquez-Abrams
A: 

PHP is preprocessed before the data is sent to the client. The server parses and executes the php code and only sends what is output by the code. If you want to get the full file you would need to have it not processed by php, for instance by renaming the file extension to something else.

Andy E
+1  A: 

First off, you shouldn't pull <script> elements via Ajax, they are not going to be interpreted and if they are, they are going to break everything on the second request.

Do some debugging by calling /Dev2/includes/view-monthly-calendar-ajax.php in your browser first. Check out the source code of what you are getting. PHP runs before the document is delivered, so there is no way to pull the HTML, but not the contents generated by PHP. They are one entity.

You either have an error in your PHP code that leads to the wrong output, or your PHP code is not being interpreted at all, which is easy to spot because the original PHP source code will be in the document's source code (which must never, ever happen).

Pekka
I can move those scripts to the main page header, so that's no problem. When I load /Dev2/includes/view-monthly-calendar-ajax.php in my browser, I get the exact same source code that I put there in my question (the one with no php). The problem is that I'm needing to interperate that php in order to build the monthly calendar when calling it via ajax...EDIT: I know that the actual code works because I am using it now in a separate page-I'm just trying to use ajax to not have to navigate to this new page and instead just replace the contents.
Joel
In your PHP code, you are running a loop ` for($i = 1; $i < $numDays+1; $i++, $counter++)` Where does `$numDays` come from? Because if that is not set or zero, the loop won't run once.
Pekka
well even if I add a $numDays = 0; things don't seem to change. I have a feeling that Paul Creasey is on the right track that I need to have the ajax call implement another page to load the php information and then load that? I just need to figure out how the heck to do that now...
Joel
Humour me and try `$numDays = 10`
Pekka
Gladly. :) My output doesn't change, unfortunately. I'm sure this is a simple thing I'm missing, but I'm still too new at this to fully "get it". Because this is a more complex problem that the singular issues I usually try and address here, I'm going to add the full index page of this to see if there is something obvious...I hope no-one minds!
Joel
@Joel No problem. The problem is definitely in your PHP code, and not in the fact that it's being called through AJAX. I suspect some variables are missing that influence what PHP queries.That is the direction you need to look into I think.
Pekka
A: 

You should implement another php page such as...

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

Then in that page you need to implement a method to load the php file as a string and return it. at the moment the php is being interpreted as you would expect.

Paul Creasey
That makes sense...I need the php code there to build the calendar that will populate the div contents being replaced. Can you point me to something that would show me how to do this?
Joel
If your hoping to process the php in the browser you can't.
Paul Creasey
I'm just hoping to make this work. What can accomplish this?
Joel