Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    pass 2 arrays from PHP to JAVSCRIPT function

    im newbie in javascript .. i need help
    i have 2 arrays in PHP:

    Code:
        print_r ($b);
        print_r ($a);
    i want to use both of the arrays to javascript function :


    Code:
        echo "<SCRIPT LANGUAGE='javascript'> gettemplate('$array1');</SCRIPT>\n";
    i want to pass $a array and $b array to this function (gettemplate) , how i do that ? by using json ? i didn't find any examples of passing 2 arrays to javascript function ..

    THANKS !

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    PHP Code:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
    <title>phpArrays to JS</title>
    <script>
    myArray = 

    <?php
    $array0 
    = array(
        
    "foo1" => "bar",
        
    "bar1" => "foo",
    );
    $array1 = array(
        
    "foo2" => "bar",
        
    "bar2" => "foo",
    );
    echo 
    "[".json_encode($array0).",".json_encode($array1)."]";

    ?>

    alert(myArray[0].foo1)
    alert(myArray[1].foo2)



    </script>
    </head>
    <body>
    </body>
    </html>
    Last edited by DaveyErwin; 06-06-2012 at 04:45 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •