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.