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

时间:2015-12-11 03:24:00

标签: javascript php jquery html ajax

我有一个小问题,弄清楚为什么这个ajax响应没有正确更新。我有一个php while循环列出了文本格式的gallerys,我正在使用。它从php页面获取详细信息,但只有一个结果,所以当你将鼠标悬停在名称上时会弹出一个qtip工具提示框,以便您可以编辑图库的名称。问题是它只列出循环中所有结果的一个结果。

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();
    });
});

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

看看这三个结果是如何相同的。

3 个答案:

答案 0 :(得分:1)

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

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

每次你打电话给$('。EditGallery')。 $( 'EditGallery')。是一个数组,你需要将循环中$('。EditGallery')的所有引用更改为$(this):

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

答案 1 :(得分:1)

您可能需要创建动态ID,以便您可以调用正确的ajax。 请修改您的ajax电话,如下所示

 $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>';

在while循环中替换此代码。

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

试着让我知道

答案 2 :(得分:1)

尝试:

 $('.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/