[英]Laravel append data after success ajax
I have this div: 我有这个股利:
@foreach($bids as $b)
<div class="bids notice notice-lg">
<strong>{{$b->user->name}}</strong> siūlo <span style="font-size: 18px" class="label label-success">{{$b->bid}} €</span>
</div>
@endforeach
And this is my AJAX: 这是我的AJAX:
$(function(){
$('#placeBid').on('submit',function(e){
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf_token"]').attr('content')
}
});
e.preventDefault(e);
if($("#jsBidPrice").val() <= $(".minBid").val()){
$.ajax({
method:"POST",
url: $("#placeBid").attr("action"),
data:$(this).serialize(),
dataType: 'text',
success: function(data){
$('.minBid').val(0);
$("#bidSubmit").attr("disabled", true);
},
error: function(data){
console.log("request failed");
}
})
}
else {
alert('Too low.');
}
});
});
How can I append my div #bids with new submitted data ? 如何在div #bids后面附加新提交的数据? This is what I'm trying to right now - I'm trying to create new DIV and place it in foreach value.
这就是我现在想要的-我正在尝试创建新的DIV并将其置于foreach值中。 Like this:
像这样:
var html = '<strong>{{$b->user->name}}</strong>';
html += 'siūlo <span style="font-size: 18px" class="label label-success">';
html += '54044 €</span>';
$('.bidsA').append(html);
And I place this div over the bids div. 我将此div放在出价div上。 So the HTML looks like this:
因此,HTML如下所示:
@foreach($bids as $b)
<div class="bidsA notice notice-lg">
</div>
<div class="bids notice notice-lg">
<strong>{{$b->user->name}}</strong>siūlo <span style="font-size: 18px" class="label label-success">{{$b->bid}} €</span>
</div>
@endforeach
And of course I get undefined variable $b
how should I correctly include it from AJAX to HTML ? 当然,我会得到
undefined variable $b
如何正确地将其从AJAX包含到HTML中?
EDIT 编辑
public function store(BidRequest $request, $id)
{
$product = Product::where('id', $id)->first();
$bid = new Bid([
'product_id' => $product->id,
'bid' => $request->bid,
]);
Auth::user()->bid()->save($bid);
}
}
You could return the view and refresh the part that contain the bids. 您可以返回视图并刷新包含出价的零件。
In controller return $bids
to view
and it will construct the html with all bids divs : 在控制器中返回
$bids
view
,它将使用所有出价divs构造html:
public function store(BidRequest $request, $id)
{
....
$bids = Bid::orderBy('bid', 'DESC')->paginate(10);
return view('products.show', compact('bids'));
}
Then in JS you will recieve the html you have just to append it to the parent container : 然后在JS中,您将收到只需将其附加到父容器的html:
success: function(data){
$('.bids_container').replaceWith(data);
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.