I have some columns in mysqli that I'm reading from PHP. It's fetching and echoing perfectly.
$results = mysqli_fetch_assoc(mysqli_query($conn, $querystring));
echo json_encode($results);
//$results = {"title":"Sea Shells","location":"./Sea Shells.txt","type":"text"}
however, javascript/jquery then reads the echo as a string:
var contentarr = [];
(ajax magic here, success: function(results){
contentarr = results;
});
contentarr[0] = {
contentarr[1] = "
how could I directly read an associative array from PHP and map it to an associative array in Javascript? Jquery is the only library I'm using.
1 Answer 1
Change
contentarr = results;
To
contentarr = JSON.parse(results);
https://www.w3schools.com/js/js_json_parse.asp
This converts to a javascipt object.
answered Dec 16, 2018 at 1:43
lufc
2,0402 gold badges16 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Omar
truly a blessing
default
json_[de|en]code()is JSON.stringify and JSON.parse - medium.com/mindorks/…header('Content-Type: application/json');) in php.