我知道这只能通过 AJAX 实现,但我从未使用过 AJAX...在我的网站上,您可以保留游戏 Magic 中的卡牌列表,这就是完整的列表。您必须按一个按钮才能添加卡片,每张卡片都有这个按钮,当您添加它时,它会将卡片添加到您的列表中,然后刷新并再次位于页面顶部。我怎样才能让它保持它的位置?
在红色方 block 内,您可以按添加或删除: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>';
}
最佳答案
在插入期间添加带有时间戳的列。 并获取时间戳升序的数据。 显示卡片。它不会改变。
关于javascript - 如何在刷新时保持我在页面上的位置(AJAX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28040938/