【问题标题】:How can I mantain my position on the page on refresh (AJAX)如何在刷新时保持我在页面上的位置(AJAX)
【发布时间】:2015-01-20 08:44:58
【问题描述】:

我知道这只能通过 AJAX 实现,但我从来没有使用过 AJAX...在我的网站上,您可以保存您在万智牌游戏中拥有的卡牌的列表,这就是列表。您必须按一个按钮才能添加一张卡片,每张卡片都有这个按钮,当您添加它时,它会将卡片添加到您的列表中,然后刷新并再次位于页面顶部。我怎样才能让它保持它的位置?

在红色方块内,您可以按添加或删除:http://prntscr.com/5uq6ak

Functions.php(我只会展示 2 个函数,移除和添加卡片)

//Add card to collection
function addCardToCollection($conn, $userID, $cardID){
//Checks if the cards is already added for this user
    $queryGetCard = 'SELECT user_id, card_id FROM collection WHERE user_id = '.$userID.' AND card_id = '.$cardID;
    $checkCollection = $conn->query($queryGetCard);
    if($checkCollection->fetch_assoc() > 0){return 'Deze kaart hebt u al.';}

//Adds card to the database
    $queryAddCard = 'INSERT INTO collection (user_id, card_id) VALUES ('.$userID.','.$cardID.')';
    if($conn->query($queryAddCard)){return 'Kaart toegevoegd.';}
    else{return 'Kaart niet toegevoegd.';}
}

//Remove card from collection
function removeCardFromCollection($conn, $userID, $cardID){
//Checks if the cards is in the collection
    $queryGetCard = 'SELECT user_id, card_id FROM collection WHERE user_id = '.$userID.' AND card_id = '.$cardID;
    $checkCollection = $conn->query($queryGetCard);
    if($checkCollection->fetch_assoc() == 0){return 'Deze kaart hebt u nog niet.';}

//Remove card from the database
    $queryAddCard = 'DELETE FROM collection WHERE user_id = '.$userID.' AND card_id = '.$cardID;
    if($conn->query($queryAddCard)){return 'Kaart verwijderd uit uw collectie.';}
    else{return 'Kaart niet verwijderd uit uw collectie.';}
}

Set.php(添加和删除按钮)

if(login_check($mysqli) == true) {
    $cardsHTML.='<br><b>Deze kaart heb ik...
    <a href="' . $baseURL . 'set.php?id=' . $_GET['id'] . '&cardID=' . $value['id'] . '&collection=add">
        <div class="glyphicon glyphicon-ok green"></div>
    </a> |
    <a href="' . $baseURL . 'set.php?id=' . $_GET['id'] . '&cardID=' . $value['id'] . '&collection=remove">
        <div class="glyphicon glyphicon-remove red"></div>
    </a>
    </b>';
}

【问题讨论】:

  • 需要将游戏状态存储在某个地方... localStorage 或服务器。
  • 如何...?我对此一无所知
  • 取决于如何确定位置。总体问题很模糊

标签: javascript php jquery html ajax


【解决方案1】:

在插入时添加带有时间戳的列。 并获取时间戳的升序数据。 显示卡片。它不会改变。

【讨论】:

    【解决方案2】:

    你有没有想过用 JS 来做这件事?

    注意:这需要 JQuery 插件,不过你可以从很多 CDN 中获得它,其中 google 是最可靠的Here之一

    $(document).ready(function(){
        var storePosition = {     /// initializing the storeposition
            topCoordinate : null
        }
    
    
         storePosition.topCoordinate =  $(this).offset().top;   /// storing the position
    
    
        if(storePosition.topCoordinate !== 0){   // if we are at the top no point doing anything
    
         $("html, body").animate({"scrollTop":  storePosition.topCoordinate}, 500);
    
    
            }
    });
    

    感谢This question

    还请注意...请停止使用非参数化查询。考虑使用 MySQLi,因为它与 MySql 语法相比没有太大的进步,并且会给你一些面向对象编程的良好实践。

    【讨论】:

    • 是的...我没有做我只是一个必须做这些小修复的实习生,我应该把代码放在哪里因为我真的不知道
    • 我会冒昧地猜测您在网站上运行了 Jquery,在这种情况下,将该代码包装在页面上的 标记中
    猜你喜欢
    • 2021-04-17
    • 2018-12-22
    • 1970-01-01
    • 2020-11-28
    • 2013-07-12
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多