Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    hmg
    hmg is offline
    New Coder
    Join Date
    Apr 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Passing data between Javascript and PHP

    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?

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have used php data in javascript by echoing the variable where needed in javascript such as
    Code:
    elem.innerHTML = <? echo $d; ?>
    I am told this is bad practice, but I have had much success in doing it.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by davehaz
    I have used php data in javascript by echoing the variable where needed in javascript such as
    Code:
    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

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Except that is short-tags aren't enabled, <?= won't work, so it makes your code immediately less portable...

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay
    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

  • #6
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    They are being deprecated in PHP6.

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    As far as I know, it's only the ASP-style tags (<%) that are being deprecated...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •