2

i have sample array $arras below. when we click on the button, the function user_info() will be called and this array will be passed to ajax request.my script looks like

<?php 
   $arr = array(
            array(
               "first_name" => "Darian",
               "last_name" => "Brown",
               "age" => "28",
               "email" => "[email protected]"
          ),
         array(
              "first_name" => "John",
              "last_name" => "Doe",
              "age" => "47",
              "email" => "[email protected]"
         )
      );

   $encoded =json_encode($arr);

?>

<button onclick="user_info()">click here</button>
<div id="result_view"></div>

and my js function is

  function user_info()
  {   
    var my_arr = '<?php echo $encoded; ?>';
    var data='';
    $.ajax({
        data: {arr: my_arr},
        type: 'POST',
        url: site_url+'admin/profile/users/',
        success: function(response)
        {
            var JSONObject = $.parseJSON(response); 
            for (var key in JSONObject) {
                if (JSONObject.hasOwnProperty(key)) {
                   data += (JSONObject[key]["first_name"] + ", " + JSONObject[key]["age"])+"<br/>";
                }
              }
            $('#result_view').html(data);               
          }
      }); 
    }

my php code and js function are on the same php file and it's working. now i want to put this js function to my external js file. and i need to pass my array as a parameter on function call. i tried to pass the array as a parameter like this

<button onclick="user_info(<?php echo $encoded;?>)">click here</button>

and my external js function look like this

function user_info(my_arr)
{
 ...
}

but it's not working. how can i pass my array as a parameter on external function call? I would appreciate for any help. thank you.

4 Answers 4

1

HTML file

<script>
    var enc = <?php echo $encoded;?>;
</script>
<button onclick="user_info()">click here</button>

External JS file

function user_info()
{
    ...
    //use enc var here
    ...
}

Just make sure the external JS file is included after the little script tag.

Sign up to request clarification or add additional context in comments.

Comments

1

Use single quotes in your array to prevent conflict with double quotes in html attributes. e.g.

   array(
               'first_name' => 'Darian',
               'last_name' => 'Brown',
               'age' => '28',
               'email' => '[email protected]'
          )

1 Comment

The quotes there don't matter because he's converting it to JSON with json_encode.
1

Pass it as string

<button onclick="user_info('<?php echo $encoded;?>')">click here</button>

2 Comments

this will become a string and you will lose the object
Yes I know this but this is the better way he can use console.log(JSON.parse(value)); to again convert it to array.
1

You shouldn't put quotes around <?php echo $encoded ?>. That's creating a string instead of an object. So it should be:

var my_arr = <?php echo $encoded; ?>;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.