Jump to content


getting a php array into javascript - help needed

  • Please log in to reply
1 reply to this topic

#1 glennn.php

  • Validating
  • PipPipPip
  • Advanced Member
  • 251 posts
  • LocationAtlanta, GA USA

Posted 09 July 2006 - 10:56 PM

i need to get an array into this javascipt, and i'm unable - can someone offer a hand?

global $onMouseOver, $onMouseOut, $picsArray, $textArray, $picUrl;                                   
                $idx = 0;
                while ($idx <= 6) {
                        $pic  = $picsArray[$idx];
                        $purl = $picUrl[$pic];

                        $text  = $textArray[$idx];

                <script language="JavaScript" type="text/javascript">

                  Text[8]=["<?php print "$text"; ?>","next line..."]


$line = "<td width=\"100\" background=\"stars/".$pic."\"><a href=\"".$phpLink."\" target=\"_blank\">
<img src=\"stars/gold.gif\" width=\"100\" height=\"100\" border=\"0\" onMouseOver=\"stm(Text[8],Style[5])\" onMouseOut=\"htm()\" /></a></td>";
                        print $line."\n";

#2 willfitch

  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 10 July 2006 - 02:59 AM

Hello Glenn (I think)0,

Although the purpose of PHP is the capability of embedding with HTML (or JS in your case), I like to stick with concatinating the output within PHP variables.  Doing this will fix your problem easily. 

Remember you can still embed the PHP with JS, but eventually I believe you will like this way better. 

One other point of assistance I can help you with is to use single quotes whenever possible.  When your page is interpreted, double quotes are first read to check for embedded items, then read as a string.  Single quotes are read as a literal, and you can just escape and concatenate your items within it like so:

$name = 'Will';
echo 'My name is '.$name.'.  How are you?';

Ok.  Let's look at this:
// Beginning of our JS content
$js = '<script language="javascript" type="text/javascript">';
// Create the "Text" variable array
$js .= 'Text = new Array();';
while ($idx <= 6) {
     $js .= 'Text['.$idx.']='.$textArray[$idx].';';
$js .= '</script>';

This will help you build your JS array and allow you to access it via the "Text" variable. 

Is this the answer you are looking for?

-- Will http://www.phpfever.com/
Zend Certified Engineer (ZEND001989)

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users