Mariyath
Mariyath

Reputation: 21

how to send array as function argument from php to javascript

I need to pass an array as a function argument from php to js.i am getting the values from database.

while ($rows = pg_fetch_array($qry))
        {
            ?>
            <option  value="<?php echo $rows['relation_name_local']?>">
            <?php echo $rows['relation_name_local']?>
            </option>
            <?php
            $app_relation_array[] =  $rows['relation_name_local'];
        }?>

i want to pass $app_relation_array[] values through addNewRow() this function Can anyone please help me with this. Thanks a lot.

Upvotes: 2

Views: 93

Answers (2)

user1607528
user1607528

Reputation:

Use json_encode() function and echo it in javascript

    while ($rows = pg_fetch_array($qry))
    {
        ?>
        <option  value="<?php echo $rows['relation_name_local']?>">
        <?php echo $rows['relation_name_local']?>
        </option>
        <?php
        $app_relation_array[] =  $rows['relation_name_local'];
        $new_data = json_encode($app_relation_array[]);
    }?>

And in your html inside script tag

     <script>
        var data = JSON.parse("<?php echo $new_data; ?>");
        alert(data);
     </script>

Upvotes: 2

Pedro Fillastre
Pedro Fillastre

Reputation: 922

Use json_encode function in php

Upvotes: 0

Related Questions