我一直在尝试编写一个阅读列表,您可以在其中添加您喜欢的文章,然后单击顶部的小x将其从列表中删除。我使用JQuery .click从数据库中删除故事,将其从列表中删除。但是,当我尝试一次删除多个故事时,它只删除最后一个单击的故事。我不知道它会是什么。我认为它可能是全局变量和局部变量,但我不知道从哪里开始。
以下是一些HTML,一个类似于我删除的div:
<li id="arrayorder_146">
<div class="reading_list drop-shadow lifted">
<div class="reading_list_container">
<a class="deletefromrl" data-storyid="146" data-parent="arrayorder_146">x</a>
<p class="reading_list_story">
<a class="rlstory" href="" data-storyid="146">iSteal: Apple Products Cause NYC Crime Wave
</a></p></div></div></li>
这是JQuery函数:
$(document).ready(function(){ $(function() {
$('a.deletefromrl').click(function(){
$(this).closest('li').fadeOut('slow');
var order = $('#list ul').sortable("serialize") + '&update=update' + '&id=' + $(this).attr('data-storyid');
$.post("deletefromReadingList.php", order, function(theResponse){
$("#response").html(theResponse);
});
这是php deletefromReadingList:
<?php
require_once('compress.php');
require_once('startsession.php');
require_once('admin_start_session.php');
require_once('db_connect_variables.php');
require_once('input_variables.php');
$array = $_POST['arrayorder'];
$id = $_POST['id'];
if ($_POST['update'] == "update")
{
$user_id = $_SESSION['user_id'];
$admin_id = $_SESSION['admin_id'];
$listorder = '';
$i=0;
foreach ($array as $idval)
{
if(($idval == $id) && ($i == 0))
{
$i++;
continue;
}
$listorder = $listorder . $idval . ',';
}
$listorder = substr_replace($listorder ,"",-1);
if(!empty($user_id) && empty($admin_id))
{
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "UPDATE reading_list SET listorder = " . $listorder . " WHERE user_id = " . $user_id;
mysql_query($query) or die('Error, insert query failed');
echo 'Saved until 9AM Tomorrow';
}
else if(empty($user_id) && !empty($admin_id))
{
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "UPDATE reading_list SET listorder = '" . $listorder . "' WHERE admin_id = " . $admin_id;
mysqli_query($dbc, $query) or die('Error, insert query failed');
echo 'Saved until 9AM Tomorrow';
}
else
{
$_SESSION['readinglist'] = $listorder;
setcookie('readinglist', $listorder, mktime(6, 59, 59, date('m'), (date('d')+1), date('y'))); // expires at midnight
echo 'Saved until 9AM Tomorrow';
}
}
?>