简体   繁体   English

在不刷新页面的情况下从数据库中获取信息

[英]Get information from the database without refreshing the page

I am developing a program.我正在开发一个程序。 In the store section, we display products for users I want you to see that product when a user is browsing the site and a new product has been added to the site without refreshing the page在商店部分,我们为用户展示产品我希望您在用户浏览网站时看到该产品,并且在不刷新页面的情况下将新产品添加到网站

I have come and refresh the contents of the store section every few seconds with the setInterval method, but it does not work for me我已经用 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>

Here's a sample code:这是一个示例代码:

<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>

Note that in the PHP URL to fetch data, it should return a complete html of the products, that'll simply get appended in the already rendered list.请注意,在 PHP URL 中获取数据时,它应该返回完整的 html 产品,这些产品将简单地附加到已呈现的列表中。 The controller logic should be such that if no data is found, then response.data is just an empty string. controller 逻辑应该是这样的,如果没有找到数据,则response.data只是一个空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不刷新同一页面的情况下刷新页面上的数据库信息 - Refresh database information on page without refreshing the same page 从数据库中检索数据而不刷新页面 - retrieve data from database without refreshing the page 从数据库重新加载数据而无需刷新页面 - Reload data from database without refreshing page 刷新div以从数据库检索信息,而无需刷新整个页面 - refresh div to retrieve information from database without refreshing wholepage 如何在不使用 GET 数据中的数据刷新页面而不单击按钮的情况下显示 mysql 数据库 - How to display a mysql database without refreshing the page with data from GET data without clicking button 如何在没有刷新页面的情况下根据选择框值从codeigniter中获取数据库中的值? - how to get values from database in codeigniter based on select box value without refreshing page? 如何使用Ajax和Php从数据库获取实时数据而无需刷新页面? - How to get real-time data from database without page refreshing using Ajax and Php? 如何在不使用$ _GET并在页面中显示信息的情况下从数据库中获取信息 - How can I fetch information from database without using $ _GET and showing information in page 拉取api信息而不刷新页面 - Pull api information without refreshing the page 在PHP中更新信息而无需刷新页面 - Update information in PHP without refreshing page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM