I decided to put this here instead of the Ajax/JavaScript Category, for I feel the problem lies more in PHP. Anyways, I have developed a online calendar that works pretty darn good and would like to add Ajax to the mix. This way the page doesn’t have to refresh to see the next month. However, I also want it so that if someone disables JavaScript that it will still work, hence the Graceful Degradation. If I can solve this for the Calendar then I will be able to implement this to other PHP applications.
Some of the code might be hard to understand that I show, but what I am mainly concerned about is the PHP-Ajax-JQuery (JavaScript) portion.
In the main page (index.php) I have this:
[php]/* This makes sures that the input is a number */
$page = filter_input(INPUT_GET, ‘page’, FILTER_VALIDATE_INT);
if (!$page) {
$page = 0;
}
//$page = filter_input(INPUT_POST, ‘page’, FILTER_VALIDATE_INT);
$setMonth = new CalendarControls($page);
$currentMonth = $setMonth->returnDate;
if ($user) {
$userName = $user->username;
} else {
$userName = NULL;
}
$month = new MyCalendar($currentMonth, $userName);
[/php]
and this
[php]
<?php echo $month->outputForm; ?>
<?php echo $setMonth->returnControls; ?>
In a Class I have this form my controls:
[php]<?php
class CalendarControls extends MyCalendar {
public $returnDate;
public $displayControls;
public $returnControls;
protected static $todaysDate;
protected static $page;
protected static $varPage;
protected static $next;
protected static $previous;
public function __Construct($page) {
$this->returnDate = $this->todaysDate($page);
$this->returnControls = $this->calButtons();
}
protected function todaysDate($page) {
self::$todaysDate = date('F Y'); // Today's Month && Year:
self::$page = isset($page) ? $page : NULL;
self::$varPage = isset(self::$page) ? self::$page : 0;
self::$previous = isset(self::$page) ? self::$page - 1 : -1;
self::$next = isset(self::$page) ? self::$page + 1 : 1;
return $_SESSION['calendarDay'] = parent::currentMonthPosition(self::$todaysDate, self::$varPage);
}
protected function calButtons() {
$this->displayControls = ‘
$this->displayControls .= ’ ’ . PHP_EOL;
$this->displayControls .= ’ ’ . PHP_EOL;
$this->displayControls .= ‘
return $this->displayControls;
}
}[/php]
The only reason I’m showing this is that this is how it changes using straight PHP using the anchor tag.
Here’s my jQuery (JavaScript) file:
[php]$(function() {
var $forward = $(’.pic-slide-right’),
$reverse = $(’.pic-slide-left’),
page = 0;
$forward.on(‘click’, function(e) {
e.preventDefault();
page += 1;
var params = { page : page }; // set parameters:
var sendPage = jQuery.param( params ); // Set parameters to object format:
savePage(sendPage);
});
$reverse.on(‘click’, function(e) {
e.preventDefault();
page -= 1;
var params = { page : page }; // set parameters:
var sendPage = jQuery.param( params ); // Set parameters to object format:
savePage(sendPage);
});
function savePage(sendPage) {
console.log(sendPage);
$.ajax({
type:“post”,
url:“currentMonth.php”,
data: sendPage,
success:function(info) {
console.log(info);
}
});
}
}); // End of Doc Ready Function:
[/php]
I’ve created a test PHP file:
[php]<?php
require ‘lib/includes/utilities.inc.php’;
$page = filter_input(INPUT_POST, ‘page’, FILTER_SANITIZE_SPECIAL_CHARS);
$setMonth = new CalendarControls($page);
$currentMonth = $setMonth->returnDate;
echo $currentMonth;[/php]
I have no problem sending the JQuery variable to PHP via Ajax when it is in a separate php file; however, when I try to send it to the index.php file it doesn’t work as expected. I know the problem is that PHP since it’s server-side loads before the client-side JQuery, but I know there has to be a way to have JavaScript with Graceful Degradation also. I’m at wits end trying to figure this out :-\ Any help will be greatly appreciated.
Thanks John