Jump to content


Photo

Passing data between Javascript and PHP


  • Please log in to reply
3 replies to this topic

#1 hmg

hmg
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 04 May 2006 - 01:14 PM

I'm new to the world of PHP, MySQL and Javascript, but have many years of programming experience in mainframe techologies. I'm developing a web application in which monthly calendars of events are to be generated. When the page is initially loaded, the current month's calendar is generated through Javascript by using the onload event to initiate the Javascript function. The user can then move forward to the next month or backward by clicking an arrow which initiates the proper Javascript function through the onclick event. For each month, I need to be able to retrieve server-side data (using PHP/MySQL) to fill each day-cell ("td"+i, below) of the calendar with events for that day which reside in the server-side database, keyed by date. Here is a snippet of the Javascript code that generates the days of a given month:

var d = 1;
for (i = 0; i < 42; i++) { //42 possible positions for dates - max num of weeks in month = 6
var elem;
var elem = eval(document.getElementById("td"+i)); //the day cell
elem.innerHTML = "";
if (i >= startingPos && i < lastpos){ //startingPos= first day position of the month; lastpos=last position +1)
elem.innerHTML = d
d += 1;
.
.
return;

In short, I need to get data from the server-side database as the days are created (elem.innerHTML = d, above) or get all the data for the chosen month (probably more efficient), and send it back to the Javascript where it can be parsed and loaded to the appropriate cell(s) in the calendar on the client side.

Can anyone please share some ideas on how to accomplish this in an easy to understand way?

THANKS!
[size=1]

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 04 May 2006 - 01:19 PM

For something like that, you should really consider AJAX, or loading the entire JS array with calendar events before the page is loaded (not really preferred, but is an option).

If you're not familiar with AJAX, might I suggest our sister site? www.ajaxfreaks.com

Info: PHP Manual


#3 hmg

hmg
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 May 2006 - 03:04 PM

Thanks for the suggestion. AJAX seems like the ideal solution, if I can get it to work.

I read Gast's "Simple Introduction to AJAX and XMLHttpRequest" and tried to use the XMLHttpRequest object to pass the date for which I need to get the server side data. Right now I'm just trying to echo the date passed from the Javascript to the server side. Here's the code as it looks now:

This is the Javascript in the HTML page:
function setToday() {
.. code to determine current month - see first posting, then:

// Make the XMLHttpRequest object
var http = createRequestObject();
window.http = http;
sendRequest();

function createRequestObject() {

var req;

if(window.XMLHttpRequest){
// Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
// There is an error creating the object,
// just as an old browser is being used.
alert('Problem creating the XMLHttpRequest object');
}

return req;

function sendRequest() {
var yymm;
var mmdb = window.month + 1
if (mmdb <= 9)
mmdb = "0" + mmdb;
yymm = window.year + "-" + mmdb;
// Open PHP script for requests
http = window.http
http.open('get', 'getevents.php?yymm='+yymm);
// http.onreadystatechange = handleResponse; will deal with response later
http.send(null);

}


getevents.php looks like this:
<?php
$host = "localhost";
$login_name = "mylogin";
$password = "mypw";

//Connecting to MYSQL
$link=MySQL_connect("$host","$login_name","$password");

//Select the database we want to use
MySQL_select_db("mydb",$link) or die("Could not select database");
$date=$_GET['yymm'];
echo ("date passed is "+$date);
mysql_close($link);
?>

I'm not getting any errors, but neither am I seeing the date echoed from the server side.

What am I doing wrong?

THANKS!!


#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 May 2006 - 03:50 PM

I suggest you post this question over there. Your login here and there are the same.

Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users