Earlier I had posted my problem with getting a single variable to javascript. It was resolved using var jstest='<?php echo $test?>';.....I now need to get an array to an equivalent array in javascript and everything I try fails! I am a newbie to php but everything I try fails.
The code below has the array $data I would like to pass to js. The code may not be perfect but it is echoing the values. Please, I would like an example of how to do this. Thanks in advance. Bob

<?php
$filename = "linksfile.txt";
$page[0] = "index.html";
$page[1] = "about.htm";
$page[2] = "news.htm";
$page[3] = "meminfo.html";
$page[4] = "contact.html";
$page[5] = "golf.htm";
$page[6] = "tennis.htm";
$page[7] = "fanda.htm";
$page[8] = "dining.htm";
$page[9] = "social.htm";
$page[10] = "weddings.htm";
$page[11] = "banquets.htm";
$page[12] = "contact.htm";
$page[13] = "PDFgallery.htm";
$page[14] = "faq-home.htm";
$green = "grdot.png\n";
$yellow = "yedot.png\n";
$none = "nodot.png\n";
$lfh = fopen($filename, 'w') or die("can't open file");
flock($lfh, LOCK_EX);
$i = 0;
while ($i < count($page))
{
$pagename =trim($page[$i]);
$linkdate = date ("Y-m-d ", filemtime($pagename));
$diff = (strtotime(date("Y-m-d")) - strtotime($linkdate)) / (60 60 24);

echo "file ".$pagename." Datem: ".$linkdate. " Diff: ".$diff."<br>";
if ($diff < 3)
{
fwrite($lfh, $green);

}

else if ($diff < 8)
{

fwrite($lfh, $yellow);
}
else
{

fwrite($lfh, $none);
}
$i++;
}
flock($lfh, LOCK_UN);
fclose($lfh);
}

// read file into array
$data = file($filename) or die("Could not read file!");
// loop through array and print each line
foreach ($data as $line)
{
echo $line,"<br>";
}
// print file contents
echo "-- ALL DONE --<br>";
?> :mad:

    First, please ALWAYS use [ PHP] and [ /PHP] (without the spaces) to insert code...

    Second, you seem to pass the contents of a file DIRECTLY to JS, without setting any variables, or calling any functions... that of course can't work !

    foreach ($data as $line)
    {
    echo $line,"<br>";
    }

    Replace it with this...

    // Let's declare the script element according to X(HT)ML rules...
    echo '<script type="text/javascript">' . "\n";
    echo '// <![CDATA[' . "\n";
    echo '  var arrLines = new Array();' . "\n";
    
    // Let's go through $data starting with element 0 to element lenght - 1
    for($intL = 0; $intL < count($data); $intL++) 
    {
    
    // Let's add backslahes to quotes (" => \", ' => \') and backslashes (\ => \\)
    $strLine = addslashes($data[$intL]);
    
    // Let's replace the actual \n and \r to their representation...
    // We don't want a carriage return at the end of the line before putting the quote !
    $strLine = str_replace("\n", '\n', $strLine);
    $strLine = str_replace("\r", '\r', $strLine);
    
    // Let's print each line using this syntax :
    //  arrLines[Position of the element] = "contents of the line with extra backslashes and transformed carriage returns";
    printf(('  arrLines[%s] = "%s";' . "\n"), $intL, $line);
    }
    
    // Let's declare the closing part of the script element...
    echo '// ]]>' . "\n";
    echo '</script>' . "\n";

      Suntra:
      Thanks for your help, however I still cannot get the javascript to read the variable. Below is how I have coded your suggestion. below that is a copy of the results.....I guess I am a little dense but I do not get the variable.
      Pig: I am sorry but I did not follow the heredoc at all.

      <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
      <html><head>

      </head>
      <BODY>
      start of html
      <script language="javascript" type="text/javascript">
      <?php
      $filename = "linksfile.txt";
      $page[0] = "index.html";
      $page[1] = "about.htm";
      $page[2] = "news.htm";
      $page[3] = "meminfo.html";
      $page[4] = "contact.html";
      $page[5] = "golf.htm";
      $page[6] = "tennis.htm";
      $page[7] = "fanda.htm";
      $page[8] = "dining.htm";
      $page[9] = "social.htm";
      $page[10] = "weddings.htm";
      $page[11] = "banquets.htm";
      $page[12] = "contact.htm";
      $page[13] = "PDFgallery.htm";
      $page[14] = "faq-home.htm";
      $green = "grdot.png\n";
      $yellow = "yedot.png\n";
      $none = "nodot.png\n";
      $lfh = fopen($filename, 'w') or die("can't open file");
      flock($lfh, LOCK_EX);
      $i = 0;
      while ($i < count($page))
      {
      $pagename =trim($page[$i]);
      $linkdate = date ("Y-m-d ", filemtime($pagename));
      $diff = (strtotime(date("Y-m-d")) - strtotime($linkdate)) / (60 60 24);

      echo "file ".$pagename." Datem: ".$linkdate. " Diff: ".$diff."<br>";
      if ($diff < 3)
      {
      fwrite($lfh, $green);

      }

      else if ($diff < 8)
      {

      fwrite($lfh, $yellow);
      }
      else
      {

      fwrite($lfh, $none);
      }
      $i++;
      }
      flock($lfh, LOCK_UN);
      fclose($lfh);
      }

      // read file into array
      $data = file($filename) or die("Could not read file!");
      // loop through array and print each line
      // Let's declare the script element according to X(HT)ML rules...
      echo '<script type="text/javascript">' . "\n";
      echo '// <![CDATA[' . "\n";
      echo ' var arrLines = new Array();' . "\n";

      // Let's go through $data starting with element 0 to element lenght - 1
      for($intL = 0; $intL < count($data); $intL++)
      {

      // Let's add backslahes to quotes (" => \", ' => \') and backslashes (\ => \\) 
      $strLine = addslashes($data[$intL]); 
      
      // Let's replace the actual \n and \r to their representation... 
      // We don't want a carriage return at the end of the line before putting the quote ! 
      $strLine = str_replace("\n", '\n', $strLine); 
      $strLine = str_replace("\r", '\r', $strLine); 
      
      // Let's print each line using this syntax : 
      //  arrLines[Position of the element] = "contents of the line with extra backslashes and transformed carriage returns"; 
      printf(('  arrLines[%s] = "%s";' . "\n"), $intL, $line); 

      }

      // Let's declare the closing part of the script element...
      echo '// ]]>' . "\n";
      echo '</script>' . "\n";

      // print file contents
      echo "-- ALL DONE --<br>";

      ?>
      end of html
      </script>
      </BODY>
      </HTML>

      RESULT:
      start of html ' . "\n"; // print file contents echo "-- ALL DONE --
      "; ?> end of html

        You inserted <script ...> before my code and </script> after it... but I already put them in the code... so you get something like this :

        <script ...>
        <script ...>

        </script>
        </script>

        So of course, JS will return an error...

        Will think a bit more tomorrow... 😉

          Write a Reply...