dongyan1841
2015-12-11 03:24
浏览 43

Ajax响应在php循环时没有更新

I am having a slight problem figuring out why this ajax response isn't updating properly. I have a php while loop which lists gallerys in text format, i am using . It is getting details from the php page but only for one result so essentially when you hover over the name a qtip tooltip box pops up so you can edit the name of the gallery. The problem is it only lists one result for all results in the loop.

PHP & HTML

    <?php
            $MemberGalleriesQuery = $bapcity->query("SELECT * FROM CroMemberRetailGalleries WHERE UserID='".$_SESSION['user_id']."' ORDER BY GalleryID DESC");
            $MemberGalleriesCount = $MemberGalleriesQuery->num_rows;

            if ( $MemberGalleriesCount ) 
            {
                $BaseHeight = 150;
                $GalleriesBoxHeight = $BaseHeight + ( 20 * $MemberGalleriesCount );
                echo '
                <div id="ManageGalleries" style="height: '.$GalleriesBoxHeight.'px" align="center">
                <div id="ManageGalleriesHeader">Manage Galleries</font></div><br><br>
                <font color="#000000"><b>Click Gallery To Edit</b></font><br><br>
                ';

                while($GalleryData = $MemberGalleriesQuery->fetch_assoc())
                {
                    echo '>> <b><a class="EditGallery" href="Crowork.Backend/Crowork.EditGallery.php?action=EditGallery&gallerykey='.$GalleryData['GalleryID'].'">'.$GalleryData['GalleryName'].'</a></b> <<<br>';
                }

                echo '<br><br></div>';
            }
            $MemberGalleriesQuery->free();
            ?>

JAVASCRIPT:

    //Edit Form When Hovering Over Gallery Name
$('.EditGallery').each(function() {


    var link = $('.EditGallery').attr('href'); //Gets link url

    $.ajax({ //Make the ajax request
        url: link,
        cache: false
    }).done(function( html ) { //On complete run tooltip code

        //Display tooltip code goes here, returned text is variable html
    $('.EditGallery').qtip({
        content: {
            text: html
        },
        hide: {
            fixed: true,
            delay: 300
        },
        style: 'wiki'
    });
    $('.EditGallery').qtip('click', true);
    $(".EditGallery").page();
    });
});

CONTENTS OF Crowork.Backend/Crowork.EditGallery.php

    if ( isset( $cleanGet['action'] ) && $cleanGet['action'] == 'EditGallery' ){

    $MemberGalleriesQuery = $bapcity->query("SELECT * FROM CroMemberRetailGalleries WHERE GalleryID='".$cleanGet['gallerykey']."' AND UserID='".$SessionUserID."' ORDER BY GalleryID DESC");

    $MemberGalleriesCount = $MemberGalleriesQuery->num_rows;

    if ( $MemberGalleriesCount )
    {       
        $GalleryData = $MemberGalleriesQuery->fetch_assoc();
    }?>

    <form action="Crowork.Backend/Crowork.EditGallery.php?action=DoEditGallery&gallerykey=<?php echo $GalleryData['GalleryID']?>" method="post">
        <input type="hidden" name="GalleryName" value="<?php echo $GalleryData['GalleryName']?>">
        <input type="hidden" name="GalleryID" value="<?php echo $GalleryData['GalleryID']?>">
        <input type="submit" name="DeleteGallery" value="Delete Gallery">
    </form>

    <form action="Crowork.Backend/Crowork.EditGallery.php?action=DoEditGallery&gallerykey=<?php echo $GalleryData['GalleryID']?>" method="post">
    <table border="0" width="100%">
    <tr>
        <td colspan="2" align="center"><font size="-1"><b>NOTE:</b> Letters & Numbers Only</font></td>
    </tr>
    <tr>
        <td>Name:</td>
        <td><input type="text" name="GalleryName" size="30" value="<?php echo $GalleryData['GalleryName']?>"></td>
    </tr>
    <tr>
        <td align="right" colspan="2">
            <input type="hidden" name="OriginalGalleryName" value="<?php echo $GalleryData['GalleryName']?>">
            <input type="hidden" name="GalleryID" value="<?php echo $GalleryData['GalleryID'] ?>">
            <input type="submit" name="EditGallery" value="Edit Gallery">
        </td>
    </tr>
    </table>
    </form>
    <?php }?>

PREVIEW:

http://www.bigjohn863.com/mini-upload-form/uploads/ajaxproblem.png

See how all three are the same results.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douduan4116 2015-12-11 05:31
    已采纳

    Try:

     $('.EditGallery').each(function()
    {
            $(this).qtip({
            content:  {
                text: "Loading...",
                ajax: {
                    url:$(this).attr('href'),
                    type: "GET",
                    success: function(data, status) {
                      this.set('content.text', data);
                    }                
                   }
    
                },
            hide: {
                fixed: true,
                delay: 300
            },
            style: 'wiki'
        });
        $(this).qtip('click', true);
    });
    

    http://jsfiddle.net/st0j8nLy/

    点赞 评论
  • duandi8852752 2015-12-11 04:43
    //Edit Form When Hovering Over Gallery Name
    $('.EditGallery').each(function() {
    
    var link = $('.EditGallery').attr('href'); //Gets link url
    ....
    

    everytime you are calling $('.EditGallery'). $('.EditGallery'). is an array, you need to change all references to $('.EditGallery') that is within the loop to $(this):

    var link = $(this).attr('href'); //Gets link url
    
    点赞 评论
  • dpw70180 2015-12-11 05:07

    You Might be need to create dynamic id so you can call proper ajax. please modify your ajax call as below

     $gid=$GalleryData['GalleryID'];              
     echo '>> <b><a onclick="editgalary($gid)" id="EditGallery_$gid" href="Crowork.Backend/Crowork.EditGallery.php?action=EditGallery&gallerykey='.$GalleryData['GalleryID'].'">'.$GalleryData['GalleryName'].'</a></b> <<<br>';
    

    replace this code in while loop.

    function editgalary(galaryid){
       var link = $('#EditGallery_'+galaryid).attr('href');
    //ajax code as it is.
    }
    

    try and let me konw

    点赞 评论

相关推荐 更多相似问题