I am trying to take a PHP array and pass it into a JavaScript function, so I can send it in a GET request. I tried to use JSON encode to pass it through, but nothing seems to work. JavaScript does not accept the array.
Here is my code, in the steps I took to get to the issue.
My array looked like this:
Array ([0] => this [1] => is [2] => data )
So I decided to use the best solution for the job, json_encode
Now my array looks like this in PHP:
$array = ["this","is","data"]
I then made my javascript function with the get request
function getRequestFunction(array){
$('#div').load("path/path/section="&array="+encodeURI(array));
}
Next I called the function and passed the function in.
getRequestFunction('<?php echo $array; ?>');
So I have passed my array to my JavaScript function. Unfortunately, when I run this code, my console says:
Uncaught SyntaxError: Invalid or unexpected token
I thought, as my array is now JSON, it should pass to the function? Can JavaScript not accept JSON as a parameter? How else can I get this array to my function, in a format to be passed as a GET request?