简体   繁体   English

使用 ajax 和 php 从表中加载更多数据

[英]load more data from table using ajax and php

I have 7 data in my database我的数据库中有 7 个数据

I try to load my data from table using ajax and php .我尝试使用ajaxphp从表中加载我的数据。

but after I click the button load more the data is showing but load more button is gone.但是在我单击按钮加载更多数据后显示但load more按钮消失了。

here my index.php code https://github.com/jazuly1/piratefiles/blob/master/index.php这里我的 index.php 代码https://github.com/jazuly1/piratefiles/blob/master/index.php

here my ajax code这是我的 ajax 代码

<script>
$(document).ready(function(){
    $(document).on('click','.show_more',function(){
        var ID = $(this).attr('id');
        $('.show_more').hide();
        $('.loding').show();
        $.ajax({
            type:'POST',
            url:'getData.php',
            data:'idpost='+ID,
            success:function(html){
                $('#show_more_main'+ID).remove();
                $('.tutorial_list').append(html);
            }
        }); 
    });
});
</script>

and my getdata.php code https://github.com/jazuly1/piratefiles/blob/master/getdata.php和我的 getdata.php 代码https://github.com/jazuly1/piratefiles/blob/master/getdata.php

before I click load more button在我点击加载更多按钮之前

在此处输入图片说明

after I click load more button.在我点击加载更多按钮后。 the button is gone.按钮不见了。

在此处输入图片说明

When "Show More" is clicked, you $('.show_more').hide() which makes sense.当“显示更多”被点击时,你$('.show_more').hide()这是有道理的。 But you never show it again?但你再也没有展示过? You also completely remove a similar selection ( $('#show_more_main'+ID).remove() ) but as far as I can tell, that doesn't have any effect.您还完全删除了一个类似的选择( $('#show_more_main'+ID).remove() ),但据我所知,这没有任何影响。

Change the remove() to something like:remove()更改为:

$('.show_more').show();
$('.loding').hide();

The problem is you have removed the Load more section with jQuery and expecting it from the getData API call.问题是您已经使用jQuery删除了Load more部分,并期望从getData API 调用中获得它。 But the getData API call returns html with <tr>...</tr> 's content and in addition Show more section, if needed.但是getData API 调用会返回带有<tr>...</tr>内容的 html,如果需要,还会返回Show more部分。 It is better to either use JSON response and generate those <tr>...</tr> 's manually in Java Script, or a JSON response with two sections, may be in below format:最好使用JSON响应并在 Java Script 中手动生成那些<tr>...</tr> ,或者包含两个部分的JSON响应可能采用以下格式:

{
    'content': '<tr><td>...</td></tr>....<tr><td>...</td></tr>',
    'showMore': '<div class="show_more_main" ...</div>'
}

Than from the response (do not forget to use response = $.parseJSON(response) ) read the content and append as $('.tutorial_list').append(response.content) for data section and similarly $("table.table").append(response.showMore) for show more section.比从响应(不要忘记使用response = $.parseJSON(response) )读取content并附加为$('.tutorial_list').append(response.content)数据部分和类似的$("table.table").append(response.showMore)显示更多部分。

I believe you got the idea, where you are doing wrong.我相信你明白了,你哪里做错了。

But if you still want to use your own code instead of JSON , just use the below trick.但是如果您仍然想使用自己的代码而不是JSON ,只需使用以下技巧。

Step 1: In getdata.php update show more section with below code (Keeping the Show more section invisible):步骤 1:在getdata.php使用以下代码更新显示更多部分(保持Show more部分不可见):

<?php endforeach;
    if($data > $showLimit){ ?>
        <div class="show_more_main" style="visibility:hidden" id="show_more_main<?php echo $tutorial_id; ?>">
            <span id="<?php echo $tutorial_id; ?>" class="show_more" title="Load more posts" href="#" style="text-decoration: none;">Show more</span>
            <span class="loding" style="display: none;"><span class="loding_txt"><i class="fa fa-spinner fa-pulse fa-3x fa-fw" style="font-size:12px;"></i>Loading....</span></span>
        </div>
    <?php }   
    } 
}?>

Step 2: Move the hidden section Show more to the right place using Java Script in index.php第 2 步:使用index.php Java Script将隐藏部分Show more移动到正确的位置

<script>
$(document).ready(function(){
    $(document).on('click','.show_more',function(){
        var ID = $(this).attr('id');
        $('.show_more').hide();
        $('.loding').show();
        $.ajax({
            type:'POST',
            url:'getData.php',
            data:'idpost='+ID,
            success:function(html){
                $('#show_more_main'+ID).remove();
                $('.tutorial_list').append(html);
                //Move to new right place if show more section exists. You may give data table an id if you want
                $(".show_more_main").appendTo("table.table");
                $(".show_more_main").show();
            }
        }); 
    });
});
</script>

I hope this helps you.我希望这可以帮助你。 Feel free to write comment, if any part is not clear to you.如果任何部分对您来说不清楚,请随时写评论。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM