【问题标题】:Ajax in controller not working properly in php控制器中的 Ajax 在 php 中无法正常工作
【发布时间】:2021-12-13 17:17:26
【问题描述】:

我正在使用 jquery 和 php,我在控制器中使用 ajax,第一次点击后我得到 结果成功(当前“显示更多按钮”隐藏并将通过 ajax html 获得新的“显示更多按钮”)但如果我点击 再次点击“显示更多”按钮(来自 ajax 响应为 html)然后没有任何效果,我错了,这是我的 代码,如果存在多个评论并使用 ajax 脚本,则会显示此功能“显示更多按钮”

<?php
function fetch()
{
    $FeedId; (dynamic)
    $FeedData = $this->M_main->GetFeedData(); // getting feed data from database
    foreach($FeedData as $feeds) {
        if($feeds['flag']=="feed") {
            $GetFeedComments = $this->M_main->GetFeedsComment($FeedId);
            $TotalFeedsComments=$GetFeedComments['TotalRows'];
            if($TotalFeedsComments>1) {
                $Loads='<div class="show_more_main" id="show_more_main'.$postID.'">
                        <input type="hidden" name="fixs" value="'.$postID.'" id="fixs">
                        <input type="hidden" name="MinValue" value="'.$postID.'" id="MinValue">
                        <input type="hidden" name="FeedIdd" value="'.$FeedId.'" id="FeedIdd">
                        <input type="hidden" name="MaxValue" value="'.$postID.'" id="MaxValue">
                        <span id='.$postID.' data-val='.$postID.' data-status='.$postID.' class="show_more" title="Load more posts">Show more</span>
                        <span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span>
                        </div>';
            } else {
                $Loads="";
            }
        }
    }
    $pathss= base_url()."Main/GetFeedCommentsById"; 
    echo "<script>
            $('.show_more').unbind().click(function(e) {
                e.preventDefault(); 
                var ID = $(this).attr('id');
                    var vals=$(this).data('val');
                    var status=$(this).data('status');
                    var fixs=$('#fixs').val();
                    var MinValue=$('#MinValue').val();
                    var MaxValue=$('#MaxValue').val();
                    var FeedIdd=$('#FeedIdd').val();
                    $('.show_more').hide();
                    $.ajax({
                        type:'POST',
                        url:'".$pathss."',
                        data:{id:ID, vals:vals,status:status,fixs:fixs,MinValue:MinValue,MaxValue:MaxValue,FeedIdd:FeedIdd},
                        success:function(html){
                            $('#show_more_main'+ID).remove();
                            $('.postList'+ID).append(html);
                        }
                    });
                
                });
            </script>";     
}
?>

这是我的函数“GetFeedCommentsById”,它通过 html 获取 cmets(与之前相同),但如果我这次点击“显示更多”按钮,那么对我没有任何作用,我错在哪里?

function GetFeedCommentsById()
{
    if (!empty($_POST["id"])) {
        $showLimit = 5;
        $fixs = $_POST['fixs'];
        $min = $_POST['id'];
        $max = $_POST["fixs"];
        $MaxValue = $_POST['MaxValue'];
        $MinValue = $_POST['MinValue'];
        $FeedId = $_POST['FeedIdd'];
        $TotalCount=$this->db->query("SELECT COUNT(*) as num_rows 
                            FROM un_feeds_comment 
                            WHERE id NOT BETWEEN $MinValue AND $MaxValue 
                            AND FeedId=$FeedId 
                            ORDER BY id DESC")->row_array();
        $totalRowCount = $TotalCount['num_rows'];
            
        $query = $this->db->query("SELECT uc.id,uc.FeedId, uc.comment, 
                                    uc.CreatedAt, u.username,u.profile_img 
                                    FROM un_feeds_comment uc 
                                        JOIN un_users u ON u.wallet_address=uc.from_wallet 
                                    WHERE uc.id NOT BETWEEN $MinValue AND $MaxValue 
                                    AND uc.FeedId=$FeedId 
                                    ORDER BY uc.id DESC 
                                    LIMIT $showLimit");
            
            
        $queryn = $this->db->query("SELECT max(id) as MaxId 
                                    FROM un_feeds_comment 
                                    WHERE id NOT BETWEEN $min AND $max 
                                    AND FeedId=$FeedId 
                                    ORDER BY id DESC 
                                    LIMIT $showLimit");
            
        $querymin = $this->db->query("select min(id) as MinId 
                                        from un_feeds_comment 
                                        WHERE id NOT BETWEEN $min AND $max 
                                        AND FeedId=$FeedId 
                                        GROUP BY id 
                                        ORDER BY id DESC 
                                        LIMIT $showLimit");
            
        $result= $query->result_array();
        $TotalRecords=count($result);
        if ($TotalRecords > 0) {
            foreach($result as $row) {
                //echo "<pre>";print_R($re);
                $postID = $row['id'];
                $FeedID = $row['FeedId'];
?>
                        <div class="list_item"><?php echo $row['comment']; ?></div>
                        
<?php
            }
        }
            
        $maxss= $queryn->row_array();
        $MaxssRecords=count($maxss);
        if ($MaxssRecords > 0) {
            $MaxId = $maxss['MaxId'];
        }
        $DefaultValue = $_POST['fixs'];
        $_POST['fixs'];
        //echo "<br>";
        $MaxId;
        if ($MaxId == "" || $MaxId < $DefaultValue) {
            $MaxId = $DefaultValue;
        } else {
            $MaxId = $MaxId;
        }
            
        $minss= $querymin->row_array();
        $MinnRecords=count($minss);
        if ($MinnRecords > 0) {
            $MinIds = $minss['MinId'];
    
            $DefaultValue = $_POST['fixs'];
            if ($postID > $MinValue) {
                $postID = $MinValue;
            }
            if ($MinIds == "" || $MinIds < $postID) {
                $postID = $MinIds;
            }
        }
?>
<?php 
        if ($totalRowCount > $showLimit) { 
?>
        <div class="show_more_main" id="show_more_main<?php echo $postID; ?>">
        <input type="hidden" name="fixs" value="<?php echo $_POST["fixs"]; ?>" id="fixs">
        <span id="<?php echo $postID; ?>" data-val='<?php echo $_POST["id"]; ?>' class="show_more" title="Load more posts">Show more</span>
                        
        <input type="hidden" name="MaxValue" value="<?php echo $MaxId; ?>" id="MaxValue">
        <input type="hidden" name="MinValue" value="<?php echo $postID; ?>" id="MinValue">
                        
        <span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span>
        </div>
<?php
        } 
?>
<?php   
    }
}
?>

【问题讨论】:

    标签: javascript php jquery ajax codeigniter


    【解决方案1】:

    虽然它不是最干净的解决方案,但可以快速解决此问题:

    $('.show_more').unbind().click(function(e) {
    

    作者:

    $(document).on('click', '.show_more', function(e) {
    

    【讨论】:

    • 我做了,但后来它工作了多次,这意味着通过单击向我显示结果超过 1 次,这就是我使用 unbind 的原因
    猜你喜欢
    • 1970-01-01
    • 2013-08-02
    • 2013-07-26
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    相关资源
    最近更新 更多