如何在视图中更新数据库中的数据?

时间:2017-05-23 13:52:27

标签: javascript php jquery laravel laravel-5

使用laravel 5.2。我在视图中从数据库打印货币值:

<div class="hidden">
@foreach ($currencies as $currency)
    <div class="currency-{{ $currency->id }}">
        <div class="cur-id">{{ $currency->id }}</div>
        <div class="cur-name">{{ $currency->cur_name }}</div>
        <div class="cur-sell">{{ $currency->cur_sell + 0 }}</div>
        <div class="cur-buy">{{ $currency->cur_buy + 0 }}</div>
        <div class="cur-reserve">{{ $currency->cur_reserve + 0 }}</div>
        <div class="cur-short">{{ $currency->cur_short }}</div>
        <div class="cur-wallet">{{ $currency->cur_wallet }}</div>
    </div>
@endforeach
</div>
@endsection

上面的代码列出了数据库及其字段的所有货币。问题是 - 如何在不使用Ajax重新加载页面的情况下更新这些值?我需要用ajax发出GET请求,然后用实际的数据替换数据。

1 个答案:

答案 0 :(得分:0)

You can try some thing like this
===================================
In your ajax fnction
$.ajax({
url : 'getresult',
method : 'get',
dataType : 'Json',
data : formdata, 
success : function(data){
// console.log(data.length);
$.each(data, function( key, value ) {
for(var i=0; i< data[key].length; i++){
$(".currency").html("");
$(".currency").append(
'<div class="cur-id">'+value[i].id+'</div>');
$(".currency").append(
'<div class="cur-name">'+value[i].name+'</div>');
}
});
}, 
error : function(){
alert('error');
}
});