try to pass json_encode variable in php to javascript. get an uncaught error in javascript. It works before, not changing anything, and now it doesnt work?!?
JSON variable is fine from PHP. Try to print_r in php, OK. Browser networks also OK.
variables I want to pass in PHP:
$response['id'] = $id;
$response['name'] = $name;
$response['note'] = $note;
$response['image'] = $image;
$response['bumbu'] = $bumbu;
$response['banyak'] = $banyak;
$response['masak'] = $masak;
$response['images'] = $images;
print_r($response);
json_encode($response);
my javascript:
$('#viewResep')
.on('show.bs.modal', function (e) {
var id = $(e.relatedTarget)
.data('id');
console.log('Requested : ' + id);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
// document.getElementById("demo").innerHTML = this.getAllResponseHeaders();
}
};
xhttp.open('GET', 'process_post.php?view=' + id, true);
xhttp.send();
fetch('process_post.php?view=' + id)
.then(text => JSON.parse(text))
.then((data) => {
r_Name = data.name;
r_Note = data.note;
r_Image = data.image;
r_Bumbu = data.bumbu;
r_Banyak = data.banyak;
r_Masak = data.masak;
r_Images = data.images;
});
});
I want to get all variables passed using json_encode in my javascript
print_routput, and discarded thejson_encoderesult. Removeprint_rand addechoto the next line.print_r()and justecho json_encode($response);XMLHttpRequestandfetch()since they're both doing fundamentally the same thing. Just usefetch(`process_post.php?view=${encodeURIComponent(id)}`).then(res => res.json()).then(data => { ... })echo,printor otherwise output anything other thanecho json_encode($response);. At the moment, it looks like it's printing some HTML. Please see the duplicates linked at the top of your question