我在Blade视图中创建了此函数,该函数从API返回数据。如何将这些数据作为值传递给HTML输入字段?
我的内联php代码:
@php
function getPrice()
{
$url = "https://bitpay.com/api/rates";
$json = json_decode(file_get_contents($url));
foreach($json as $obj)
{
if($obj->code == 'USD')
$btc = $obj->rate;
}
echo $btc;
}
@endphp
我要将上述数据传递到的HTML输入字段:
<p class="form-group">
<label for="subject">Amount in BTC<span class="required">*</span></label>
<i class="fa fa-btc "></i>
<input type="text" aria-required="true" size="30" value="" name="btc" id="btc" class="form-control" placeholder="Amount in Bitcoin" readonly="true" >
</p>
答案 0 :(得分:0)
确定要使用function()
吗?
您可以直接离开:
@php
$btc = null; (?)
$url = "https://bitpay.com/api/rates";
$json = json_decode(file_get_contents($url));
foreach($json as $obj)
{
if($obj->code == 'USD')
$btc = $obj->rate;
}
@endphp
及之后:
<input type="text" aria-required="true" size="30" value="{{ $btc }}" name="btc" id="btc" class="form-control" placeholder="Amount in Bitcoin" readonly="true" >
答案 1 :(得分:0)
我已经更新了这篇文章。您无需创建函数。都不回显该变量。就是这样:
<?php
$url = "https://bitpay.com/api/rates";
$json = json_decode(file_get_contents($url));
foreach($json as $obj){
if($obj->code == 'USD')
{
$btc = $obj->rate;
}
}
?>
并假定两个代码都在同一刀片文件中。您只需像这样调用变量:
<input type="text" aria-required="true" size="30" value="" name="btc" id="btc" class="form-control" placeholder="Amount in Bitcoin" readonly="true" value = "{{$btc}}" >