我有一个尝试获取api数据(price
)的函数。
问题:如何将这些数据传递到视图并使用该数据和视图选择选项上的某些选定选项执行数学计算?
//我的功能
public 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;
}
return $btc;
}
//我的jQuery
<script type="text/javascript">
$(.calculate).change(function(){
var pkg = parseInt($('.form-control.pkg').val());
var day = parseInt($('.form-control.day').val());
var price = "<?php echo $btcDisplay; ?>";
if (!isNaN(pkg) && !isNaN(day)) {
var plan = parseInt(pkg);
var duration = parseInt(day);
var total = pkg * day / price ;
}
});
</script>
//下面是我的html
p class="form-group">
<label for="name">Package type <span class="required">*</span></label>
<i class="rt-icon2-dropbox highlight3" aria-hidden="true"></i>
<select class="form-control pkg calculate" name="package" id="package">
<option value="" selected="selected">Select a amount</option>
<option value="100">test</option>
<option value="105">start</option>
<option value="120">trial</option>
<option value="150">production</option>
</select>
</p>
<p class="form-group">
<label for="name">With holding period <span class="required">*</span></label>
<i class="fa fa-hand-grab-o" aria-hidden="true"></i>
<select class="form-control day calculate" name="days" id="days">
<option value="" selected="selected">With holding period…</option>
<option value="10">10 working days</option>
<option value="30">30 working days</option>
<option value="60">60 working days</option>
</select>
</p>
<p class="form-group">
<label for="name">Number of package <span class="required">*</span></label>
<i class="rt-icon2-sort-numerically-outline highlight3" aria-hidden="true"></i>
<input type="number" name="number" class="form-control" size="30" aria-required="true" placeholder="number of package" min="1">
</p>
<p class="form-group">
<label for="subject">Amount in BTC<span class="required">*</span></label> <i class="fa fa-btc highlight2" aria-hidden="true"></i> <input type="text" aria-required="true" size="30" value="<?php echo $btcDisplay; ?>" name="btc" id="btc" class="form-control" placeholder="Amount in Bitcoin" readonly="true" >
</p>