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);
?>