I have a multidimensional array and want to send it as a parameter to JS function. Google told me that json_encode()
is the way to go, but I get an unterminated string literal error when I try to call the function.
Here's my code:
$wynik_artefakty = $polaczenie->query("SELECT * FROM artefakty");
$tablica_artefakty = mysqli_fetch_all($wynik_artefakty,MYSQLI_ASSOC);
$tablica_artefakty = json_encode($tablica_artefakty);
echo "<div class='wpis'
onclick=\"dodajDiv('$tablica_artefakty')\">"
.$tablica_jednostek[$i]['nazwa']." ".$tablica_jednostek[$i]['koszt']."</div>";
And the example echo of the array after json_encode()
:
[{"Id":"2","nazwa":"Sceptre of Atrocities","personal":"The
Gorgon","dla_maga":"0","koszt":"15"},{"Id":"3","nazwa":"Artefakt dla
maga","personal":"","dla_maga":"1","koszt":"10"}]