Page 1 of 1

PHP code inside JavaScript

Posted: Tue Jan 22, 2008 4:57 am
by aceconcepts
Hi

How can I include some PHP code inside a JavaScript?

This is what I want to do:

Code: Select all

 
<script type="text/javascript">
  function dateChanged(calendar) {  
    if (calendar.dateClicked) {
      var y = calendar.date.getFullYear();
      var m = calendar.date.getMonth();     // integer, 0..11
      var d = calendar.date.getDate();      // integer, 1..31
      
      window.location = "/and/index.php?page=appointments&action=diary&d="+d+"&m="+m+"&y="+y;
    }
    
  };
 
  Calendar.setup(
    {
 
          //THIS IS WHERE I WANT TO INCLUDE THE PHP CODE - SEE BELOW
 
      date         : $_REQUEST['d']."/".$_REQUEST['m']."/".$_REQUEST['m'],
 
      flat         : "calendar-container", // ID of the parent element
 
          flatCallback : dateChanged           // our callback function
    }
 
  );
</script>
 
the above JavaScript is held in a '.php' file.

Any ideas how this can be done?

Thanks

Re: PHP code inside JavaScript

Posted: Tue Jan 22, 2008 5:30 am
by Chris Corbyn
That will work fine if it was directly embedded into the page.

If you need a separate file, put this at the top of it.

Code: Select all

<?php header("Content-Type: text/javascript"); ?>
Then when you include it in the page:

Code: Select all

<script type="text/javascript" src="your-php-file.php"></script>