weixin_33696106 2017-11-19 20:15 采纳率: 0%
浏览 16

循环中的AJAX响应问题

I want ajax to update two different things. one is the clicked button class, and second is database record in while loop

Home

<?php
    $q = mysqli_query($cn, "select * from `com`");
    while ($f = mysqli_fetch_array($q)) {
    ?>
    com: <?php echo $f['com']; ?><br>
    <?php
        $q1 = mysqli_query($cn, "select * from `fvc` where m_id='" . $f['id'] . "' and log='" . $_SESSION['id'] . "'");
        $q2 = mysqli_query($cn, "select * from `fvc` where log='" . $_SESSION['id'] . "'");
    ?>
         <span class="result<?php echo $f['id']; ?>">
         <?php if (mysqli_num_rows($q1) > 0) { ?>
               <button value="<?php echo $f['id']; ?>" class="unfc"><i title="<?php echo mysqli_num_rows($q2); ?>" class="fa fa-star" aria-hidden="true"></i></button>
         <?php } else { ?>
               <button value="<?php echo $f['id']; ?>" class="fc"><i title="<?php echo mysqli_num_rows($q2); ?>" class="fa fa-star-o" aria-hidden="true"></i></button>
         <?php } ?>
         </span>
    <?php
      }
    ?>

AJAX

$(document).ready(function(){

            $(document).on('click', '.fc', function(){
                var id=$(this).val();
                $.ajax({
                        type: "POST",
                        url: "vote.php",
                        data: {
                            id: id,
                            vote: 1,
                        },
                        success: function(){
                            showresult(id);
                        }
                    });
            });

            $(document).on('click', '.unfc', function(){
                var id=$(this).val();
                $.ajax({
                        type: "POST",
                        url: "vote.php",
                        data: {
                            id: id,
                            vote: 1,
                        },
                        success: function(){
                            showresult(id);
                        }
                    });
            });

        });

    function showresult(id){
            $.ajax({
                url: 'result.php',
                type: 'POST',
                async: false,
                data:{
                    id: id,
                    showresult: 1
                },
                success: function(response){
                    $('.result'+id).html(response);

                }
            });
        }

result.php

<?php
        session_start();
        include('cn.php');

        if (isset($_POST['showresult'])){
            $id = $_POST['id'];
            $q3=mysqli_query($cn, "select * from `fvc` where m_id='".$id."' and log='".$_SESSION['id']."'");
            $q4=mysqli_query($cn,"select * from `fvc` where log='".$_SESSION['id']."'");
            $numFavs = mysqli_num_rows($q4);
            if (mysqli_num_rows($q3)>0){
            echo '<button class="unfc" value="'.$id.'"><i title="'.$numFavs.'" class="fa fa-star" aria-hidden="true"></i></button>' ;
            } else {
            echo '<button class="fc" value="'.$id.'"><i title="'.$numFavs.'" class="fa fa-star-o" aria-hidden="true"></i></button>' ;
                }   
            }   
    ?>

total number of row response is not updating for all comments in while loop. I want loop ids to be updated as well in Ajax response for each comment So guide me whats wrong in my code

  • 写回答

2条回答 默认 最新

  • hurriedly% 2017-11-19 21:38
    关注

    Ofcourse it will update only one element, your show_like function updates only one element, the one with id = #show_like'+id

    if you want to update all span elements, create a function update_likes() and call it in the success instead of show_like(),

    $(document).on('click', '.favcom', function(){
                var id=$(this).val();
                $.ajax({
                        type: "POST",
                        url: "like.php",
                        data: {
                            id: id,
                            like: 1,
                        },
                        success: function(){
                            update_likes('.favcom');
                        }
                    });
            });
    

    then loop through the span elements and update each one of them, you can add a class .show_like if you have other spans and instead of $("span") put your class,

    function update_likes(class){
        $(class).each(function() {
            show_like( $(this).val() );
        });
    }
    

    i hope this works, however, this is based on your code and it will make a lot of http requests ( in show_like() ) and i would recommend you improve it by trying to return all the data you need and loop through an array instead of making Ajax calls every time.

    评论

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决