PHP Array to JavaScript Array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

PHP Array to JavaScript Array?

Post by rxsid »

Hi all,

I'm trying to pass a PHP array into a javascript array. I know how to do this with a variable, just can't get the array to work without giving a js error when the page loads.

The PHP piece opens a file that contains lines with both text and <html> tags.

The PHP code snipet:

Code: Select all

$handle = fopen ("somedir/subdir/" . $somefile . ".txt", "r");

while (!feof ($handle)) {
   $theInfo[] = fgets($handle, 4096);
}
fclose ($handle);

The JavaScript code snipet (loading below the PHP piece):

Code: Select all

var messages=new Array()
messages[0]= theImg  //This loads no problem w/o the array below

<?php
   echo("messages[1] = ".$theInfo[0].";");
?>
That JS code gives a "Syntax Error" when the page loads. If I debug it, it shows the array was loaded with the info from the file:

Code: Select all

var messages=new Array()
messages[0]= theImg

messages[1] = <font color='red'>Test</font><br><font size='4' color='blue'>TESTING</font>;

I've tried using:
   echo"("messages[1] = ".$theInfo[0].";");"
   echo("messages[1] = "$theInfo[0]";");
   echo(messages[1] = ".$theInfo[0].");
All with no success.
It's having a problem with messages[1]

Does anyone have a snipet where they successfully pass a PHP array to a Javascript Array?

Thanks!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You're missing some " marks around the JavaScript value.

Code: Select all

<?php 
   echo("messages[1] = "".$theInfo[0]."";"); 
?>
Here's another example.....

Code: Select all

<?
    $list[] = "Apple";
    $list[] = "Banana";
    $list[] = "Coconut";

    echo "<script language="JavaScript">\r\n";
    echo "js_list = new Array();\r\n";

    foreach($list as $key => $value)
    {
        echo "js_list[{$key}] = "{$value}";\r\n";
    }

    echo "</script>\r\n";
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or by using join to pass the array's values to the constructor of the js-array

Code: Select all

<?php
$phpArr = array('a', 'b', 'c', 'd');
?> 
<html>
	<head>
		<script type="text/javascript">
			var jsArray = new Array("<?php echo join('", "', $phpArr); ?>");
		</script>
	</head>
	<body>
		<script type="text/javascript">
			alert(jsArray);
			for (i=0; i!=jsArray.length; i++)
			{
				document.write(jsArray[i]);
				document.write("<br />");
			}
		</script>
	</body>
</html>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

volka wrote:or by using join to pass the array's values to the constructor of the js-array
Hmmmmm... never thought of doing that before. volka does good once again.
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

Post by rxsid »

Thanks all for the replies!!

Gen-ik...that first snipet was exactly what I was looking for!

Code: Select all

<?php 
   echo("messages[1] = "".$theInfo[0]."";"); 
?>
There were so many suggestions out there and non would work. So many variations too of course.

I was trying to escape the other set of quotes...just in the wrong place (see below).

Code: Select all

<?php 
   echo("messages[1] .= "".$theInfo[0]."";"); 
?>
Also...the append .= was causing me problems also, cause even with the correct escaping of the quotes, I would get an "Expected Identifier" error. So it was a two part correction.

After looking at that darn code on and off for many weeks, I finally threw the towel in and had to post it.

Thank again...your a sanity saver!
Post Reply