www.webdeveloper.com
Results 1 to 4 of 4

Thread: passing php array to javascript onclick function?

  1. #1
    Join Date
    Dec 2004
    Posts
    439

    passing php array to javascript onclick function?

    Hello, I have a php array that I would like to send to a javascript function, like this:
    Code:
    <?php
    $arr = array('a','b','c');
    ?>
    <html>
    <a onclick="showDiv($arr)">show</a>
    </html>
    <script>
    function showDiv(arr){
    len = arr.length
    }
    </script>
    How can I do this, thanks in advance.

  2. #2
    Join Date
    Oct 2006
    Posts
    1,297
    PHP Code:
    <?php
    $arr 
    = array('a','b','c');
    ?>
    <html>
    <script>
    function showDiv(arr){
    len = arr.length
    }
    </script>
    <a onclick="showDiv(Array(<?php echo implode(','$arr); ?>))">show</a>
    </html>

  3. #3
    Join Date
    Apr 2011
    Posts
    1

    Answer : passing php array to javascript onclick function?

    hello, Here is your answer I think it will help you to solve the problem,
    <html>
    <head>

    <script>
    function showDiv(arr){
    len = arr.length;
    for(var x=0;x<3;x++){
    alert(arr[x]);
    }

    }
    </script>
    </head>

    <!-- Call JavaScript function to display array element -->
    <body>

    <?php
    $arr = json_encode(array('a','b','c'));
    ?>
    <a onclick='showDiv(<?php echo $arr;?>)'>show</a><br />
    </body>
    </html>

  4. #4
    Join Date
    Apr 2011
    Posts
    3
    implode

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles