How to pass null value via json_encode

Asked

Viewed 214 times

1

How can I pass a php json to jquery with null values. I have the structure below:

$data1 = null; $data2 = null; $data3 = null;
json_encode(array(
    'html'=>$html, 
    'data1'=>$data1, 
    'data2'=>$data2, 
    'data3'=>$data3)
);

The fact of passing null gives an error:

Uncaught Syntaxerror: Unexpected token < in JSON at position 0

PHP use 5.5.12

  • 5.6 works well https://ideone.com/fork/7hsBor. No http://phptester.net/ works with 5.5 and 5.4. Where does this variable come from $html?

  • I mount part of html in php and pass via json for the $html variable.

  • Is giving error in Jquery ?

1 answer

0


You are passing the null value correctly. The problem I see in your code is that you wrote $date3 in the json_encode() instead of $data3. Outside that is not guarding the return of json_encode. Solving this, the code worked perfectly here.

Solution:

$data1 = null;
$data2 = null;
$data3 = null;

$json = json_encode(array(
  'html' => $html,
  'data1' => $data1,
  'data2' => $data2,
  'data3' => $data3
));;
  • AP fixed it, it was a typo.

  • There was a php error for jquery. The data was out of format. Now it’s ok.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.