我想在文本字段中打印file_get_contents()
返回值。
我的表单(input.html):
<form name="" action="form.php" method="post">
<input type="text" name="number" id="number"/>
<input type="submit" name="submit" value="go"/>
</form>
这是我想要绑定的视图页面(form.php)
<?php
$number = $_REQUEST['number'];
echo $number;
$data = file_get_contents('http://apis.sdsds.sds/api/Get_Loadsheet_Details/'.$number);
?>
以下是file_get_contents()
返回值:
[{"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}]
我想在bind.php的文本字段中绑定此返回值,这是我的绑定代码:
<input type="text" name="cmpname" value="<?php echo $data[0].COMPANY_NAME?>"/>
但是它显示了警告信息,我认为在上面的文本字段中绑定有些错误。
答案 0 :(得分:4)
首先,你需要json_decode你的文字。
$data = json_decode($data);
您应该可以使用以下行正确访问您的值
<input type="text" name="cmpname" value="<?php echo $data[0]->COMPANY_NAME?>"/>
请注意,我替换了“。”用“ - &gt;” 在php中,点表示连接。
答案 1 :(得分:0)
假设您有一个json文件。通过这种方式你可以操作它,我已经将json文件保存在languages文件夹中。
<suite name="Cryptography" verbose="1" parallel="classes" thread-count="8">
,因为html表单是
<?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>
在你的例子中,你从.html转移到form.php,你想把结果放到第三个文件(bind.php),你可以留在第二个.php文件