1

I want to print file_get_contents() return value in text field.

My form (input.html):

<form name="" action="form.php" method="post">
    <input type="text" name="number" id="number"/>
    <input type="submit" name="submit" value="go"/>
</form>

This is my view page where I want to bind (form.php)

<?php 
$number = $_REQUEST['number'];
echo $number;
$data = file_get_contents('http://apis.sdsds.sds/api/Get_Loadsheet_Details/'.$number);
?>

Here is file_get_contents() return value:

[{"ID":103,"FROM_ID":1,"NAME":"CUTTACK","COMPANY_NAME":"B K TRADING","CMP_ID":8473,"LR_NO":"00107","LR_ID":752,"LMID":17,"TO_ID":4,"DESTINATION":"TALCHER","GODAWN_ID":1,"GODAWN":"BAJARKABATI ROAD","NO_OF_PKT":8.00,"TOPAY_AMOUNT":0.00,"REMARKS":"","LOADIG_STATUS":"Close","LR_STATUS":"Delivered","LOADING_SHEETNO":"00006","MANUAL_LOADSHEET_NO":"","modeof_payment":"PAID","COLLECTED_TOPAY_AMNT":0.00,"LOADFROMMST":"CUTTACK","LOADFROMMSTID":1,"DESTINATION_ID":4,"LOADDESTINATIONNAME":"TALCHER","SUFIX":"BK","MST_GODAWN":1,"GODAWNMASTER":"BAJARKABATI ROAD","LRGODAWN":"BAJARKABATI ROAD","LRSUFIX":"BK","LRGODAWNID":1,"VEHICLE_NO":"OD-05-N-3856","VEHICLEID":799,"basic_freight":320.00,"sur_charge":0.00,"hamali":16.00,"lr_charge":30.00,"service_charge":0.00,"cover_charge":0.00,"dd_charge":0.00,"dp_charge":0.00,"grand_total":366.00,"booking_incharge":"SURYA","clubpoint":0.00,"onloading_charge":0.00,"LOADSHEET_TYPE":"NORMAL","DATE":"2017-04-03T00:00:00","lrConfirmStatus":null,"lrLoadStatus":null}]

I want to bind this return value in a text field in bind.php and here is my bind code:

<input type="text" name="cmpname" value="<?php echo $data[0].COMPANY_NAME?>"/>

But it show warning message, I think some mistake in binding in above text field.

Siguza
  • 21,155
  • 6
  • 52
  • 89
dipti
  • 81
  • 1
  • 4
  • 12

2 Answers2

4

First of all you need to json_decode your text.

$data = json_decode($data);

You should be able to properly access your value with the following line

<input type="text" name="cmpname" value="<?php echo $data[0]->COMPANY_NAME?>"/>

Note that i replaced "." with "->" In php the dot means concatenation.

Dan Dumitriu
  • 226
  • 1
  • 5
0

Assume that you have a json file. With that way you can manipulate it,I have already the json file saved in languages folder.

<?php


if(isset($_POST['number'])){

$number = $_POST['number'];

$counter=0;
$mydata=0;

$data = file_get_contents('languages/en.json');

$d = json_decode($data);

    foreach ($d as $key) {

        if($counter+1==$number){

            $mydata=$key;
        }

        $counter++;
    }

}

?>
<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8">
<title>JSON example</title>

</head>
<body>
<input type="text" name="cmpname" value="<?php $mydata ?>" placeholder="<?= $mydata ?>">



</body>
</html>

as the html form is

<form name="formone" action="form.php" method="POST">
        <input type="text" name="number" id="number"/>
        <input type="submit" name="submit" value="go"/>
</form>

In your example you move from .html to form.php and you want to put the results to third file(bind.php), you can stay at second .php file