Jump to content

Need help with AJAX and PHP


Spark_Plug

Recommended Posts

Hey,

 

I am trying to make a calendar and was going off of this, http://www.evolt.org/quick_calendar_using_ajax_and_php

 

Right now, I have the calendar being displayed properly for the month, what I am having problems with is understanding the AJAX part. I am not sure where I am supposed to include it or anything like that. I am still learning PHP and wanted to hand code my own calendar which is why I am not downloading his full source code. If you need me to post up my PHP script I will do that for you.

 

Also I created it in a class and have been confused by using the $this-> variable. From what I have learned just from debugging, is it seems its needed in any of the functions if you are trying to access a variable that you declared in the beginnig or if you want to use a function in the class inside another function. Could someone please help me understand the concept a little more?

 

Last question is I have WAMP installed and when I try to access phpMyAdmin I keep getting HTTP 403 Forbidden Error. Any clue on how to fix that?

 

Thanks for the help!

Link to comment
https://forums.phpfreaks.com/topic/157630-need-help-with-ajax-and-php/
Share on other sites

AJAX is JavaScript. It should be on the calendar page already so you shouldn't need to include it. Perhaps I misunderstood what you mean by include.

 

The $this keyword refers to the object or class itself. Say you have a variable in the class called $num and you also have a function that takes $num as a parameter. Well by just calling $num, you're referring to the parameter $num. $this->num refers to the variable in the class.

 

Example

class Foo {
     private $num;
     public function foo ($num) {
          $this->num = $num; // this assigns the private $num above to the value of $num (the parameter passed)
     }
}

 

Get it? $this just refers to something in the class.

THank you for explaining the $this part.

 

I just don't know where to put the javascript on the calendar page, I am going to include the source for the two files.

 

The javascript

http.open('get', 'quick_calendar.php?m='+m+&y='+y+'&ran='+ran_no);

and

document.getElementById("quickCalender").innerHTML = http.responseText; 

 

 

testCalendar.php

<?php
class Calendar
{
//Initialize Variables
var $counter, $year, $month, $day, $daysInMonth, $firstDay, $tempDays, $weeksInMonth, $week;

function __construct(){
// Getting Todays Date

$this->year = date('Y');
$this->month = date('n');
$this->day = date('j');
}

public function fillArray()
{
	$this->daysInMonth = date('t', mktime(0,0,0, $this->month, 1, $this->year));

	// Getting first day of the month
	$this->firstDay = date("w", mktime(0,0,0, $this->month, 1,$this->year));

	// Calculating total spaces needed in array
	$tempDays = $this->firstDay + $this->daysInMonth;

	// Calculate total rows needed
	$this->weeksInMonth = ceil($tempDays/7);

	for ($i=0; $i <$this->weeksInMonth; $i++)
	{
		for ($j=0; $j<7; $j++)
		{
			$this->counter++;
			$this->week[$i][$j] = $this->counter; 
			$this->week[$i][$j] -= $this->firstDay;

			if ($this->week[$i][$j] < 1 || $this->week[$i][$j] > $this->daysInMonth)
				$this->week[$i][$j] = "&nbsp";
		}
	}
}	

public function displayCalendar()
{
	$this->fillArray();
	echo '<table>
			<tr>
				<th colspan="7">';
	echo date('M', mktime(0,0,0,$this->month,1,$this->year)) . " " . $this->year;
	echo '</th>
			</tr>
			<tr>
				<th>Sun</th>
				<th>Mon</th>
				<th>Tue</th>
				<th>Wed</th>
				<th>Thr</th>
				<th>Fri</th>
				<th>Sat</th>
			</tr>';
	for ($i=0; $i <$this->weeksInMonth; $i++)
	{
		echo '<tr>';
		for ($j=0; $j < 7; $j++)
		{
			echo '<td>';
			echo $this->week[$i][$j] . '</td>';
		}
		echo '</tr>';
	}
	echo '</table>';
}
}

?>

 

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>UWW DECA</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="header">
	<img src="images/logo.jpg" width="800" height="250"   /></div>
<div id="menu">
	<ul>
		<li><a href="#">Home</a></li>
		<li><a href="#">Item 2</a></li>
		<li><a href="#">Item 3</a></li>
		<li><a href="#">Item 4</a></li>
		<li><a href="#">Item 5</a></li>
	</ul>
</div>
<div id="content">
	<div id="left">
		<div class="post">
			<h2>Recent Updates</h2>
			<p>This will be a blog like feature.  Any news stories will be updates here.
		</div>
	</div>
	<div id="right">
		<h2>Calendar</h2>
			<?php
				require_once('testCalendar.php');
				$obj = new Calendar();
				$obj->displayCalendar();
			?>
	</div>
</div>
<div id="footer">
	<p class="copyright">Copyright © 2009 University of Wisconsin - Whitewater DECA
</div>
</div>
</body>
</html>

 

It was the first class I have written for PHP so there are probably a lot of easier ways to do some of the stuff, but what I had works fine to display a calendar for the current month.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.