Reputation: 249
I have a series of articles on my webpage. Each of these has a delete form with the same name ("article"). All of my POSTs are done through ajax and work fine! The one problem I have now is that only the last article gets deleted. The hidden input has a value of the row# in the database This is the output I get:
'deletearticle' => 'article=31&article=27&article=21',
Here's my AJAX:
$('.deleteForm').submit(function() {
$.ajax({
url: 'functions/check.php',
type: 'POST',
data: {
deletearticle: $('.deleteForm').serialize()
},
success: function(data){
window.location.reload(true);
}
});
return false;
});
And here's my delete
if (isset($_POST['deletearticle'])) {
parse_str($_POST['deletearticle'], $deletearticle);
$id = $deletearticle['article'];
try {
$stmt = $DB_con->prepare("DELETE FROM articles WHERE id = ".$id." ");
$stmt->execute();
} catch (PDOException $e) {
echo "Fail: ".$e->getMessage();
}
}
EDIT I do know about name=XXX[]. It just doesn't tell me which one in the array was picked by the user to delete.
Upvotes: 0
Views: 51
Reputation: 509
You just need to use the article[]
syntax as the form element name.
Then PHP automatically converts it to an array of paramaters.
Of course you need then to change your SQL to something like
WHERE articleId IN( ' . $articleIds . ' )
But read first about proper sql escaping http://php.net/manual/de/security.database.sql-injection.php
Upvotes: 3