0

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

7
  • 1
    You printed the print_r output, and discarded the json_encode result. Remove print_r and add echo to the next line. Commented Oct 10, 2019 at 5:35
  • remove the print_r() and just echo json_encode($response); Commented Oct 10, 2019 at 5:35
  • You do not need both XMLHttpRequest and fetch() since they're both doing fundamentally the same thing. Just use fetch(`process_post.php?view=${encodeURIComponent(id)}`).then(res => res.json()).then(data => { ... }) Commented Oct 10, 2019 at 5:41
  • 1
    Make sure your PHP script does not echo, print or otherwise output anything other than echo 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 Commented Oct 10, 2019 at 5:52
  • 1
    U are right Phil !! I accidently put print_r somewhere on top. now its working, but dont pass the array correctly to javascript. bah! Commented Oct 10, 2019 at 6:01

1 Answer 1

-2

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);
}
});
Sign up to request clarification or add additional context in comments.

14 Comments

$ajax return a not a function error
$.ajax({ type: "POST", url: "myphppage.php", success: function(result) { alert('success'); } } });
share your exact error here..
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)
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); } });
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.