I was wandering if it is possible to send a php object/array from a database into a javascript function through html?
Here is what I am trying to accomplish:
<?php
$test = array('Apple', 'Banana', 'Lemon');
?>
<a href="javascript:;" onclick="myfuntion(<?php echo json_encode($test) ?>);">Send object</a>
<script type="text/javascript">
function myfuntion(test)
{
console.log("What is first fruit: "+test[0]);
}
</script>
I get this error in my console:
Uncaught SyntaxError: Unexpected end of input
myfuntion([
What am I missing? Hoping for help and thanks in advance :-)
<a href="javascript:;" onclick="myfuntion('<?php echo json_encode($test) ?>');">Send object</a>