...

View Full Version : including PHP generated array into .js file



patty005
03-20-2004, 01:26 PM
Hello,
I am having php generate an array which needs to be accessed by a javascript function in a .js file.

I have tried including the following in the .js file:

document.write("<SCR" + "IPT LANGUAGE='JavaScript1.2' SRC='New_Arrays4_2.js.php' TYPE='text/javascript'><\/SCR" + "IPT>");

but this doesn't seem to work.

I know the output of the php file is ok because when I paste its output into the .js file it works fine.

Thank you for any help.
Patty

Willy Duitt
03-20-2004, 02:55 PM
Should work.
Looks good and is not throwing any errors.
Make sure the path to the external javascript file is correct.

What's with the .php?

.....Willy

liorean
03-20-2004, 02:59 PM
The questions is, does the server send it as the correct MIME type? PHP defaults to 'text/html', but this should be sent as 'applicaton/x-javascript'. (No, not 'text/javascript'.)

patty005
03-20-2004, 03:05 PM
the php is there because it is the php file set up to generate and print the needed data.

the js script is basically a hierarchical menu generator in which you are supposed to manually insert the menu elements.

I generated mine using the php script--it prints out this:


this.item0="Automation Systems"
this.icon_rel0= 0
this.url0='get.php?id=1'
this.item0_0="Automated Carts"
this.icon_rel0_0= 0
this.url0_0='get.php?id=2'

but don't know how to get the js file to access the data--the document .write that I used doesn't seem to work...

I will try changing the application type (I know path is ok)

thanks,
Patty

liorean
03-20-2004, 03:08 PM
Observe that this is the MIME type I'm talking about, not the contents of the type attribute of the script element. Try this:
<?php
header("Content-type: application/x-javascript;charset=utf-8");
?>

patty005
03-20-2004, 03:11 PM
sorry, but where is this placed?
Before I echo my data?
patty

liorean
03-20-2004, 03:18 PM
Anywhere in the php file.

[edit] Listen to Nightfire, not me...

Nightfire
03-20-2004, 03:25 PM
Should go above anywhere that's printed to the browser, otherwise you get a 'cannot modify header. headers already sent..' error. Next time please only do one thread on the same topic, it's wasting peoples time answering in 2 threads for the same problem

patty005
03-20-2004, 03:33 PM
sorry!
I wan't sure where was best to post (JS or php!)
thank you for your help.
patty

patty005
03-20-2004, 03:54 PM
I realize the problem now--I had <br> lines in my code.

Mozilla helped in the debugging.

Many thanks to all of you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum