1

Possible Duplicate:
Parse a JavaScript file through PHP

I have some pretty long string variables in javascript that I instantiate in a .js file that way I can use them in multiple files, and also just so it doesn't muddle up the pages by being super long. However, I want to echo out some php into the .js file (so I can use a filepath specified in my php) and put it into the variable.

I know that if it was in a normal html page I could just do <?php echo "foo"; ?> and it would work allright. But, in the js file it seems to ignore it. The js file currently instantiates a variable and an associative array that store html forms so I can put them on the page depending on some user input.

I need to echo out the php file path onto the action part of the form. THe reason for doing this is so I only have to change the file path once if I go back and modify my code. However the js file will either stick the actual text () in my variable, or the form won't load.

Any help would be appreciated and if you need some more info, feel free to ask. Thanks alot!

Community
  • 1
  • 1
samuraiseoul
  • 2,888
  • 9
  • 45
  • 65

3 Answers3

6

change the file extension of your javascript file to .php and use that file instead example

<script type="text/javascript" src="test.php"></script>

Also set the correct header on top of the file using

<?php
    header("Content-type: text/javascript");
?>
Jeffrey Monte
  • 680
  • 1
  • 5
  • 12
  • This is on the right path, Im having a little problem with it right now, it's saying that the ending is invalid XML syntax. It might have something to do with how I declare my associative array. Im declaring it like var gamearray = new Array(); then Im doing gamearray[gamename] = "text"; gamearray[gamname2] = "more text"; Is that proper syntax? – samuraiseoul Jul 11 '12 at 10:11
  • Thanks Feida!... Modified it now Samuraisoulification.. – Jeffrey Monte Jul 11 '12 at 10:15
  • Okay, were on the path because the IDE recognizes Im using php now. HOwever, the rest of the page no longer recognizes the previously mentioned array as having been declared anymore... – samuraiseoul Jul 11 '12 at 10:20
0

Forcetype your js file as php , in your .htaccess file.
Something like this ,

<Files filename.js>
SetHandler application/x-httpd-php
ForceType application/x-httpd-php
</files>
Debugger
  • 544
  • 1
  • 9
  • 25
0

It might help you 1. make .inc or php file 2. define class and function 3. write the js inside php function like

<?php
   class abc
   {
      public function sum()
      {
?>
        //your js here
<?php

      }

}
?>
  1. include this file in your required file
  2. create object and call the function
Max
  • 117
  • 4
  • 18
  • Good idea but waaaay overkill for what im wanting. Im using a MVC php framework that gets my view on the page, and the view has some js in it that has a huge array that's in a seperate file. The array stores some forms in it, and the forms need to just have some php echoing out a filepath. – samuraiseoul Jul 11 '12 at 10:12