0

I have a script that calls dat from my table and returns it in JSON format. How do I echo out the var nps and data as php variable.

My script:

$.ajax({
    type: "POST",
    url: "../charts/1-2-4-reports_nps.php?TAG=<?php echo $_SESSION['SectionVar'];?>&From=<?php echo $_SESSION['StartDate'];?>&To=<?php echo $_SESSION['EndDate'];?>",
    cache: false,
    success: function(data){
       var nps = data[0].name;
       console.log(nps);
       var data = data['data'];
       console.log(data);
       $("#div1").append($("<div/>", { id: "div2", text: nps }));
    }
});

My Json returns:

[{"name":"nps","data":[35]}]

Something like $nps = data['nps'] and echo the result of $nps.

37
  • post the json data . try json_decode() . Commented Jul 10, 2017 at 8:20
  • do you mean the ones in the success? Commented Jul 10, 2017 at 8:22
  • 2
    you can not do that this way, php is a server-side language while javascript is a client-side Commented Jul 10, 2017 at 8:22
  • @Edwin yes the ones in the success. Commented Jul 10, 2017 at 8:23
  • 1
    @ADyson again, many thanks for all your time. I have ordered two books relating to javascript and Jquery. I will and do learn. Commented Jul 11, 2017 at 20:47

1 Answer 1

1

I think initially you were confused about the contexts your code is running in. var nps = data['nps']; is JavaScript. It runs in the browser, and it runs after the page is loaded.

$nps by contrast is a PHP variable, and PHP runs on the server, and it runs before the page is loaded. In fact it is the PHP which creates the HTML, CSS, Script etc which is then downloaded to the browser. After that is downloaded, the JavaScript executes separately.

There is no direct connection between the two languages, other than you can use PHP to generate JavaScript (just like you use it to generate HTML).

Once you understand that, then you realise that to display your nps variable further down the page, you need to use JavaScript to manipulate the web page and insert the data.

The first issue to resolve with that is that the data being returned from the server is coming back as a string that looks like JSON, but not an actual JSON object. You can add

dataType: "json"

to your ajax options, and that tells jQuery to treat the returned data as an object instead of a string.

The next problem is that data is an array, containing a single object, and you were trying to read it like it was just a plain object.

So you get data out of it, and, as you requested, display each value in a new div which gets inserted into an existing div, you can do the following :

function success(data)
{
    var nps = data[0].name;
    var dt = data[0].data[0];
    $("#div1").append($("<div/>", { id: "div2", text: nps }));
    $("#div3").append($("<div/>", { id: "div4", text: dt }));
}

You can see a working example (with simulation of the ajax call, but using the correct data structure) here: https://jsfiddle.net/yukot05x/5/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.