-1

I have ajax that calls a php file called "fetch_quarter_limit.php" from template file.

     $.ajax({

         type: 'POST',
         url: 'fetch_quarter_limit.php',
         data: { 
             leavefrom: from,
             leaveto: to,
             empid: emp
         },
         success: function(data) {
            var quarter_limit = data['myVar'];
            alert(quarter_limit);
         }
     });

In my .php file i have tried to return the session data as an array. Fetched the required data, stored in session and formed an array.

$_SESSION['quarter_limit_mend'] = $quarterLimit;
$returnVal = array('myVar' => $_SESSION['quarter_limit_mend']);
echo $returnVal;

As shown in above ajax code part, i tried to fetch it, but all i am getting is "undefined" when i output the variable using alert.

Please help.

Ajax code updated, p2 :

Adding dataType is making code not to work.

$.ajax({

         type: 'POST',
         url: 'fetch_quarter_limit.php',
         dataType: 'json',
         data: { 
             leavefrom: from,
             leaveto: to,
             empid: emp
         },
         success: function(data) {
             alert(data);
    
         }
 });

As @Tim mentioned i have added custom json encode function to my .php file. It returns as expected {"myVar": 2}

echo array_to_json($returnVal);

This is returned from php file. But not able to access in ajax code.

6
  • 1
    You need to encode the array as JSON before you echo it Commented Feb 24, 2021 at 9:25
  • @ADyson Sorry, i didn't add that, let me try. Commented Feb 24, 2021 at 9:26
  • @ADyson php version in < 5, so json_encode is not available! Commented Feb 24, 2021 at 9:26
  • @ADyson I tried adding session_start();, but still it outputs undefined. Commented Feb 24, 2021 at 9:28
  • 1
    php version in < 5...then you need to upgrade urgently. You are years out of support. php.net/supported-versions.php . I guess you'll have to make the JSON manually then, or find a polyfill. Echoing an array directly in PHP will just produce a warning (or nothing, if warnings are switched off, or set to log to a file) because it can't directly convert an array to a string (and even if it could, it still wouldn't be valid JSON that Javascript could read). Commented Feb 24, 2021 at 9:31

2 Answers 2

1

You're using echo on an array, which is not possible. As described in the PHP manual echo outputs one or more strings.

Usually you'd use json_encode() on your array and then output it to the screen. But as you've commented you are using php < 5. First of all, if possible, you should consider to upgrade to PHP > 7, as this not only improves performance, it also improves security.

If you can't upgrade to a PHP version above PHP 5, then you can use workarounds. On this question there is already an answer for the workaround, and the workaround can be found on the PHP manual itself.

You should be able to use the returned JSON data.

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

4 Comments

I have added the method from the discussion. echo array_to_json($returnVal); Passing this way. But there is no success repsonse. When i remove dataType: 'json', from ajax code, the alert returns as undefined. Am i missing something.
Try using console.log() on your data instead of an alert and look in the console what it returns. console.log(data); @Akhilpaul
In jQuery dataType: 'json' automatically converts JSON to an object. So to get the value of myVar you'd do console.log(data.myVar) or alert(data.myVar)
If the succes is not being called, then there could be an error. You can use error: function() {} to get the error response
0

Reply for after your edit

So you have your data as JSON now, but JS will still see it as a string. In your success function you still have to parse it.

success: function(data) {
    jsonData = JSON.parse(data);
    alert(jsonData.myVar);
}

I do have to add too that it is very insecure to continue using php < 7.3 (as of today, 24-02-2021)

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.