I have a form where the admin can either update multiple records or delete multiple records depending on which button was clicked.
This is the form:
<form action="" method="post" name="review-form" id="review-form">
<ul id="reviews">
<?php foreach ($reviews as $key => $value): ?>
<li>
<h4><span><input name="guestbook_id[]" id="guestbook_id[]" type="checkbox" value="<?php echo $value['guestbook_id']; ?>"></span> <?php echo $value['entry_name']; ?>
<select name="source_id" id="source_id" class="source">
<?php foreach($sources as $source): ?>
<option value="<?php echo $source['source_id']; ?>"><?php echo $source['source_name']; ?></option>
<?php endforeach; ?>
</select>
<span></h4>
<p><?php echo $value['entry_comment']; ?></p></li>
<?php endforeach; ?>
</ul>
<input name="btnSubmit" type="submit" id="btnSubmit" class="update_button" value="Update entries">< <input name="btnDelete" type="submit" id="btnDelete" class="update_button" value="Delete entries">
</form>
I use the following jquery function:
$("#review-form").on("submit", function(e) {
e.preventDefault();
$.ajax({
url : "/admin/manageReviews",
type: "post",
data: $(this).serialize(),
success: function(data){
window.alert("The review table was successfully updated. Click the OK button to continue.");
setTimeout(function() {
location.reload();
}, 500)
},
error: function(data){
window.alert("Something went wrong and the online review table was not updated. Click the OK button to try again.");
setTimeout(function() {
location.reload();
}, 500)
}
});
});
This is the manageReviews action:
public function manageReviewsAction()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($_POST["btnSubmit"]))
{
$source_id = filter_input(INPUT_POST, 'source_id', FILTER_SANITIZE_NUMBER_INT);
$update = $this->page->update_guestbook_entries($source_id,$_POST['guestbook_id']);
}
elseif (isset($_POST["btnDelete"]))
{
$delete = $this->page->delete_guestbook_entries($_POST['guestbook_id']);
}
}
}
And this are the 2 methods I call in the action:
public function update_guestbook_entries($source_id,array $ids)
{
$ids = implode(', ', $ids);
$sql = "UPDATE guestbook_entries
SET source_id = ?
, isActive = 1
WHERE guestbook_id IN ($ids)";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array($source_id));
}
public function delete_guestbook_entries(array $ids)
{
$ids = implode(', ', $ids);
$sql = "DELETE FROM guestbook_entries
WHERE guestbook_id IN ($ids)";
$stmt = $this->pdo->query($sql);
$stmt->execute();
}
But what ever I try, neither the update or delete actions are executed. Am I overlooking something?
Thank you in advance