47 users online (1 members and 46 guests)  


  Results 1 to 5 of 5

Related

  1. Javascript array in PHP    Forum: PHP Forum
    Replies: 0
  2. Replies: 1
  3. Adding values to an external .js Javascript file?    Forum: Javascript Forum
    Replies: 3
  4. [SPLIT]passing values to an external .js file    Forum: Javascript Forum
    Replies: 37
  5. passing values to an external .js file    Forum: Javascript Forum
    Replies: 10
  1. #1
    hugh's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2008
    Posts
    4

    Passing PHP array from one file to Javascript array in another file

    I am trying to pass a PHP array into a javascript array. The arrays will be in different files but the javascript file is referenced in the php file.

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: Passing PHP array from one file to Javascript array in another file

    The way to do this will depend on what you are trying to do. Perhaps if you gave a few details, it would help.

  3. #3
    Maldrex's Avatar
    New User

    Status
    Offline
    Join Date
    Dec 2008
    Posts
    8

    Re: Passing PHP array from one file to Javascript array in another file

    No ways to pass php array to java script. You must generate page by using php, and write array to page like this

    PHP Code:
    <script>
    <?php

    echo 'var list = array (';
    foreach (
    $list as $key => $value) echo $key.' =>"'.$value.'"'."\n";
    echo 
    ');';

    ?>
    </script>
    Sorry for bad syntax, I dont know JavaScript

  4. #4
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: Passing PHP array from one file to Javascript array in another file

    Maldrex, you are both right and wrong. You are right in that you cannot directly pass a value from a PHP file to a Javascript page. However, if PHP is used to generate the HTML page (including the Javascript), the PHP values can easily be passed to the Javascript since the PHP creates the Javascript. You can therefore plug any value into the page you want.

    So in answer to the original question, again:

    The way to do this will depend on what you are trying to do. Perhaps if you gave a few details, it would help.

  5. #5
    Maldrex's Avatar
    New User

    Status
    Offline
    Join Date
    Dec 2008
    Posts
    8

    Re: Passing PHP array from one file to Javascript array in another file

    My example show TS how he can pass any array or variable from php script to JS, when you can include php script as out JS file or generate page on php. There is no anther ways to do what TS wont.



Tags for this Thread