简体   繁体   中英

show error message in php page when get data is null

I want to show error message when return value is null.

this is my form page where i submit lrno.

index.html

 <form method="post" name="myForm" action="tracking.php">
 <input type="text" name="number" id="number" placeholder="Enter LR Number" 
  required>
 <input type="submit" name="submit" value="Go">    
 </form>

this is my bind page where i bind data related to lrno

tracking.php

<?php

$number = $_REQUEST['number'];
$data=file_get_contents('http://apis.sd/api/Get_Loadsheet_Details/'.$number);
$datas = json_decode($data);
if($datas == "")
{
$msg = "No data avialable.";
}
?>
<input type="text" name="cmpname" value="<?php echo $datas[0]->COMPANY_NAME?>"/>
<span>
<?php if(isset($msg ))
echo $msg ;
?>
</span>

here when i submit the form it goes to tracking.php but error message not show if submited lrno no avialable in database but if i refresh tracking.php again it shows an error message.

Change

if($datas == "")
{
    $msg = "No data avialable.";
}

to

if(empty($datas))
{
    $msg = "No data avialable.";
}

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. So it is better to check with empty so that it can handle the above mentioned scenario.

instead of

if($datas == "")

Use

if (empty($var) || is_null($var))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM