Hi, I'm looking for a way to include a php file (re-named as .inc), into my drupal theme which I seem to have sorted with:
include "custom.inc"; (in template.php)

However, the php script in 'custom.inc' has javascript embeded in it using:
header('Content-Type: text/javascript'); (at the start of the custom.inc file)

This seems to confuse my page somehow and I get the entire custom.inc and html page source printing onto the page as text.

I can't find a way to include a php file into my theme that also work along side javascript, sharing variables.

Can anyone offer some guidance or suggestions.

Many thanks,
fiasst

Comments

vm’s picture

I'm no expert but could the js be pulled out put it it's own .js file and also declared in the .info rather than left in a .inc file?

otherwise you may have to use drupal_add_js() ?

fiasst-1’s picture

Hi, I should explain, the javascript works closely with the php, using it's variables. Also I need to keep the javascript behind closed doors for security reasons on this particular site. I believe separating the javascript to it's own .js file would allow end users to read it.

I'm sure I've seen modules like Hierarchical Select using javascript within php files. I just need some guidance.

jodamn’s picture

Greetings... Javascript is a client-side technology - it is run on the user's computer and executed in their web browser. Contrast this with PHP, which is run on the server to generate the output (which is sent to the client).

This means the user can never see your PHP code (unless something screws up, of course), but they can always read your javascript (although you can strip comments and change variable names, I suppose).

Edit: It's seldom a good idea to be doing security with javascript - keep as much of that on the server-side as possible.