0

I am getting some data from the server and displaying on the screen. My JavaScript is shown below, and the PHP server code follows. My problem is when the database returns null (for instance, the title). PHP recognizes it as NULL, and JavaScript in turn recognizes it as null. I then display it on the screen, and it is not displayed as null, but displayed as "null". I don't wish to display the text "null", but display nothing.

As a workaround, I can replace data.title with ((data.title)?data.title:''). Is this the best way to deal with it?

$.get('server.php',{id:1855},
    function (data)
    {
        //data={"id":"1855","firstname":"Pat","lastname":"Prentice","title":null}
        $('#my_id').html('<dl><dt>Name:</dt><dd>'+data.firstname+' '+data.lastname+'</dd>'+'<dt>Title:</dt><dd>'+data.title+'</dd>');
    },'json');

server.php returns the following:

<?php
$sql_output=array('id'=> 1855, 'firstname'=>'Pat','lastname'=>Prentice,'title'=>NULL);
echo json_encode($array);
?>
user1032531
  • 24,767
  • 68
  • 217
  • 387
  • 1
    You can't do much here, the only choice you really have is if you want to handle it on the server side or client. – sachleen Oct 09 '12 at 16:51
  • Dont forget to add `header('Content-Type: application/json');` – Lawrence Cherone Oct 09 '12 at 16:52
  • fwiw - `((data.title)?data.title:'')` will also print an empty string for `''`, `0`, `false`, `undefined` and any other falsey condition. I would recommend expanding your server side code to handle nulls more explicitly. – doublesharp Oct 09 '12 at 16:52
  • 2
    I would pass an empty string in the JSON instead of `NULL`. Javascript converts `null` to the string `"null"` (unlike PHP which converts it to an empty string) but if you want your JS to treat it as an empty string, you're best using an empty string. – DaveRandom Oct 09 '12 at 16:53
  • explanation here: http://stackoverflow.com/a/11944223/982924 – RASG Oct 09 '12 at 16:53
  • 1
    You could also set a defauly value of " " (or anything else) from your query, so that it never actually returns null. – will Oct 09 '12 at 16:56
  • Also in your example, you want `json_encode($sql_output)` - `$array` is never defined - and `Prentice` should be in quotes (unless it is a constant), `lastname=>'Prentice'` – doublesharp Oct 09 '12 at 17:02
  • Sounds like serverside is the way to go unless I don't care if '',0,false, and undefined are all treated the same. Will, I like your approach. COALESCE() seems like the ticket. – user1032531 Oct 09 '12 at 17:51

3 Answers3

1

Just for a change of form, you can also use OR e.g. dd>' + (data.title || '') + '</dd...

If title is undefined or null, the empty string would be used in place. This looks easier to read than (condition)?r1:r2 form. But both are unreliable as doublesharp points out in comments.

Abhishek Mishra
  • 5,002
  • 8
  • 36
  • 38
1

I would recommend setting the value to an empty string in your PHP code. If statements in JavaScript will check for any falsey condition.

<?php
$sql_output=array('id'=> 1855, 'firstname'=>'Pat','lastname'=>Prentice,'title'=>NULL);
foreach ($sql_output as &$row){ // pass by reference
    foreach($row as $key=>$value){
        if ($value==null) $row[$key] = "";
    }
}
echo json_encode($sql_output);
?>
doublesharp
  • 26,888
  • 6
  • 52
  • 73
0

Will kind of provided the best answer as a comment:

You could also set a defauly value of " " (or anything else) from your query, so that it never actually returns null

Since the data is being pulled from a database, just do:

SELECT COALESCE(title,'') AS title, bla, bla, bla
user1032531
  • 24,767
  • 68
  • 217
  • 387