weixin_33696106
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条回答 默认 最新

  • csdnceshi54
    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.

    点赞 评论
  • weixin_33735676
    weixin_33735676 2017-11-19 21:38

    Your success function is targetting an element by ID, so a single specific element on the page. If you want to update multiple values you need to target a class or an element type which is common to the elements you want to target.

    You have two classes for the buttons, so you could use those. You would have to change your PHP output so it just returns the new total number of likes, no HTML output needed.

    Your success function in showLike would look something like this:

    $(':button[value="'+id+'"]').toggleClass('favcom unfavcom');
    $('.unfavcom').html('<i title="Remove from Favorite? - ('+response+'/20)" class="fa fa-star" aria-hidden="true"></i>');
    $('.favcom').html('<i title="Favorite Comment - ('+response+'/20)" class="fa fa-star" aria-hidden="true"></i>');
    
    点赞 评论

相关推荐