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
Aakash Tushar
5047 silver badges22 bronze badges
1 Answer 1
Use Ajax instead of this...........
Updated->
PHP Page
$response['id'] = $id;
$response['name'] = $name;
$json = array("id" =>$response['id'],"name" =>$response['name']);
JS
$.ajax({
type: "POST",
url: "myphppage.php",
success: function(result) {
alert(result.id);
alert(result.name);
}
});
answered Oct 10, 2019 at 5:36
Ranjith Kumar
1704 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
14 Comments
Abram
$ajax return a not a function error
Ranjith Kumar
$.ajax({ type: "POST", url: "myphppage.php", success: function(result) { alert('success'); } } });Ranjith Kumar
share your exact error here..
Abram
resep.js:8 Uncaught TypeError: $.ajax is not a function at HTMLDivElement.<anonymous> (resep.js:8) at HTMLDivElement.dispatch (jquery-3.3.1.slim.min.js:2) at HTMLDivElement.v.handle (jquery-3.3.1.slim.min.js:2) at Object.trigger (jquery-3.3.1.slim.min.js:2) at HTMLDivElement.<anonymous> (jquery-3.3.1.slim.min.js:2) at Function.each (jquery-3.3.1.slim.min.js:2) at w.fn.init.each (jquery-3.3.1.slim.min.js:2) at w.fn.init.trigger (jquery-3.3.1.slim.min.js:2) at o.t.show (modal.js:119) at HTMLDivElement.<anonymous> (modal.js:535)
Ranjith Kumar
check my update answer or check here also... Use Ajax instead of this........... Updated PHP Page
$response['id'] = $id; $response['name'] = $name; $response['note'] = $note; $response['image'] = $image; $response['bumbu'] = $bumbu; $response['banyak'] = $banyak; $response['masak'] = $masak; $response['images'] = $images; $json = array("id" =>$response['id'],"name" =>$response['name']); JS $.ajax({ type: "POST", url: "myphppage.php", success: function(result) { alert(result.id); alert(result.name); } }); |
default
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