Results 1 to 4 of 4

Thread: pass a php variable to javascript

  1. #1
    Join Date
    Mar 2004

    pass a php variable to javascript

    I have a script to count down the time from a future date.
    I generate the date in PHP and pass it as a variable to the script.

    However, the way in which the javascript is written is causing me an issue, I can't put the variable in.
    I have thought of creating a new java variable and using that, but I keep hitting the same problem.

    Here is a sample of the script:

    //The next line is the target countdown value
    //It is based on LOCAL TIME to the countdown value

    targettime = new Date("July 16 2004 0:00:01");

    It is the target date in "" that is causing the issue.
    I want to replace it with <?php echo $variable; ?> eg.
    targettime = new Date("<?php echo $variable; ?>");

    the variable is generated in PHP at the beginning of the page.
    The variable does appear to put the variable value in (as viewed in source code from browser page), and even between the "", however, the script stops and does not behave as the original.

    Is there some way to create a new java variable from the PHP variable and then substitute the date for this new variable.
    Incidently, the script does not work without the date being surrounded by "".

    Being a complete novice with java I appologies if this is dead simple and you all laugh at me!

  2. #2
    Join Date
    Dec 2002
    Is the date that you're echoing out to the page correctly formatted? If it isn't formatted correctly the JavaScript won't recognize it and that could be causing your error. You can look in the PHP Manual for more information on the date() function and how to format its output.
    The Web Standards Project Build accessible standard compliant websites, please!
    Browse Happy Don't forget to support the browsers with standard compliance

  3. #3
    Join Date
    Mar 2004

    date is good

    Thanks for the response.

    The date is in exactly the same format, I have done testing by changing it around and the script does stop working, so I have made sure the format is identicle

  4. #4
    Join Date
    Nov 2003
    Aachen, Germany

    I assume, your js code is not correct. Try something like this and you will see that it works:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    ="July 16 2004 0:00:01";
    <script language="JavaScript" type="text/javascript">
    targettime = new Date("<?php echo $variable?>"); 
    Cheers - Pit

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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