Jump to content

Archived

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

hmg

Passing data between Javascript and PHP

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
I suggest you post this question over there. Your login here and there are the same.

Share this post


Link to post
Share on other sites

×

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.