Page 1 of 1

PHP Array to JavaScript Array?

Posted: Fri Oct 31, 2003 4:20 pm
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!

Posted: Fri Oct 31, 2003 4:36 pm
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";
?>

Posted: Fri Oct 31, 2003 4:38 pm
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>

Posted: Fri Oct 31, 2003 4:43 pm
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.

Posted: Fri Oct 31, 2003 4:49 pm
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!