0

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.

1
  • print_r($datas) in both cases and post it to see the structure of the response Commented May 22, 2017 at 6:07

2 Answers 2

2

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.

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

Comments

0

instead of

if($datas == "")

Use

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

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.