-1

I am trying to pull a JSON I created using this PHP code:

if ($result=mysqli_query($conn,$sql))
{
   $rowcount=mysqli_num_rows($result);
}

$myJSON = json_encode($rowcount);

echo $myJSON;

I am trying to add the value $myJSON to a Javascript variable. Currently, my code looks like this:

var maxqty; 
$.getJSON('[URL in here or .php file]', data, function(jsonData) {
maxqty = jsonData; 
}); 

I know this should be a really simple thing to do, but I can't understand where my code is going wrong. I think either my PHP file isn't outputting the correct format or my $.getJSON call is wrong. I get an error when I make my $.getJSON call saying

Uncaught reference error: data is not defined.

Any advice?

Phil
  • 157,677
  • 23
  • 242
  • 245
user955857
  • 191
  • 4
  • 14
  • Do you use `maxqty` outside the callback function? – Sverri M. Olsen Jul 10 '17 at 05:15
  • What is expected result if `$result=mysqli_query($conn,$sql)` evaluates to `false`? Use `.then()` chained to `$.getJSON()` to get value of jQuery promise object and include error handling. `$.getJSON()` returns results asynchronously. See [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323?s=1|0.8364#14220323) – guest271314 Jul 10 '17 at 05:17
  • @SverriM.Olsen Yes, I use it in an if-statement later in the same script. – user955857 Jul 10 '17 at 05:17
  • add header('Content-Type: application/json') in php file then return the result – MasoodRehman Jul 10 '17 at 05:18
  • Check in browser console & network panel whether the ajax call is a success or failure... – Shiladitya Jul 10 '17 at 05:21
  • If the error is *"Uncaught reference error: **data** is not defined"*, then it's probably in reference to `$.getJSON('[URL in here or .php file]', data...`. What is `data` here and do you even need it (it's typically for supplying query parameters for the request)? – Phil Jul 10 '17 at 05:25
  • I have nothing in data, just "data". I kept it in because I thought I had to for formating. – user955857 Jul 10 '17 at 05:26
  • 2
    If `data` is not defined, then you **cannot** use it. Simply use `$.getJSON('url', function(jsonData) { ... })`. Voting to close as a *typo* – Phil Jul 10 '17 at 05:27

3 Answers3

2

just pass header('Content-Type: application/json'); in your code

<?php
    if ($result=mysqli_query($conn,$sql))
    {
       $rowcount=mysqli_num_rows($result);
    }
    header('Content-Type: application/json');
    echo json_encode($rowcount);

also in your script

var maxqty; 
$.getJSON('http://localhost/karthi/test.php', function(jsonData) {
maxqty = jsonData; 
console.log(jsonData);
});

if you pass any values to server,assign values to data

data = 'list'; 
 var maxqty; 
    $.getJSON('http://localhost/karthi/test.php', data, function(jsonData) {
    maxqty = jsonData; 
    console.log(jsonData);
    });
karthickeyan
  • 372
  • 2
  • 12
2

At first you need to add headers in php code. header('Content-Type: application/json');

Then problem is in jquery, your code doesn't know what is data variable in

$.getJSON('[URL in here or .php file]', data, function(jsonData)

data is the argument which will be sent to the server, it could be any plane text or nothing. Just assign anything to data before pass it

1

Loading stuff from the server takes time. Setting an outside variable from inside the callback will not work as you intended.

// The request start here
var request = $.getJSON('[URL in here or .php file]', data);

var maxqty = undefined;

request.done(function (data) {
    // This callback is called when the data is done
    // loading. The "data" variable is only available
    // here, and not outside the callback

    maxqty = data;
});

// "maxqty" is "undefined" here until the request
// is done loading
console.log(maxqty);
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • I feel like whilst this is not OP's current problem, it will be their next one ;) – Phil Jul 10 '17 at 05:29
  • Further reading material here ~ https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Phil Jul 10 '17 at 05:31