[英]Get information from the database without refreshing the page
我正在開發一個程序。 在商店部分,我們為用戶展示產品我希望您在用戶瀏覽網站時看到該產品,並且在不刷新頁面的情況下將新產品添加到網站
我已經用 setInterval 方法每隔幾秒刷新一次商店部分的內容,但它對我不起作用
<div id="parnet" class="row">
<p id="players">
<?php foreach ($players as $player) { ?>
<div class="col-lg-6 col-xl-3">
<div class="card text-center">
<div class="card-body">
<div class="row m-b-30">
<div class="col-md-5 col-xxl-6">
<div class="new-arrival-product mb-4 mb-xxl-4 mb-md-0">
<div class="new-arrivals-img-contnent">
<img class="img-fluid" src="<?=baseUrl()?>/upload/images/players/<?=$player['image']?>" alt="">
</div>
</div>
</div>
<div class="col-md-7 col-xxl-6">
<div class="new-arrival-content position-relative">
<h4><?=$player['playerName']?></h4>
<p>Buy Now Price <span class="item text-success"><?=$player['buyPrice']?></span></p>
<p>Market Price: <span class="item text-success"><?=$player['marketPrice']?></span> </p>
<p>Price In Dollar: <span class="item text-success"><?=$player['price']?></span></p>
</div>
<button type="button" class="btn btn-rounded btn-primary btn-sm"><span class="btn-icon-left text-primary"><i class="fa fa-shopping-cart"></i>
</span>Buy</button>
</div>
</div>
</div>
</div>
</div>
<?php } } ?>
</p>
</div>
<script>
setInterval(function() {
$(".parent").load(document.URL+ ' #players');
}, 1000);
</script>
這是一個示例代碼:
<script>
$(document).ready(function(){
setInterval(function() {
fetchNewProducts();
}, 1000);
});
function fetchNewProducts(){
let get = $.get('<Your URL to fetch new products>', {
lastProdId: "<Insert_Max_Fetched_ProdId>",
})
get.done(response => {
response = JSON.parse(response);
if (!response.error) {
$('#targetDiv').append(response.data)
} else {
alert(response.message);
}
})
}
</script>
請注意,在 PHP URL 中獲取數據時,它應該返回完整的 html 產品,這些產品將簡單地附加到已呈現的列表中。 controller 邏輯應該是這樣的,如果沒有找到數據,則response.data
只是一個空字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.