...

View Full Version : Passing data between Javascript and PHP



hmg
05-01-2006, 08:47 PM
I'm a bit new to both Javascript and PHP. I am working on an application in which monthly calendars of events are to be generated. When the page is initially loaded, the current month's calendar is generated using Javascript; the user can also move forward to the next month or backward. 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 code that generates the days of a given month:

var d = 1;
for (i = 0; i < 42; i++) {
var elem;
var elem = eval(document.getElementById("td"+i)); //the day cell
elem.innerHTML = "";
if (i >= startingPos && i < lastpos){
elem.innerHTML = d
d += 1;
.
.
return;

In short, I need to get data from the 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.

Can anyone please share some ideas on how to accomplish this in a not-too-complicated way?

davehaz
05-02-2006, 12:20 AM
I have used php data in javascript by echoing the variable where needed in javascript such as


elem.innerHTML = <? echo $d; ?>

I am told this is bad practice, but I have had much success in doing it.

Noodles24
05-02-2006, 09:05 AM
I have used php data in javascript by echoing the variable where needed in javascript such as


elem.innerHTML = <? echo $d; ?>

I am told this is bad practice, but I have had much success in doing it.

It's not bad practice at all, but you should be using <?=$d?> instead, which does the same job

GJay
05-02-2006, 09:55 AM
Except that is short-tags aren't enabled, <?= won't work, so it makes your code immediately less portable...

Noodles24
05-02-2006, 12:31 PM
Except that is short-tags aren't enabled, <?= won't work, so it makes your code immediately less portable...

Aren't they enabled by default on most systems? I've never come across a system that hasn't had them enabled and i've never edited php.ini to enable them

degsy
05-02-2006, 03:21 PM
They are being deprecated in PHP6.

GJay
05-02-2006, 05:27 PM
As far as I know, it's only the ASP-style tags (<%) that are being deprecated...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum