Crazy Horse
asked on
foreach issue when using php MVC/ajax/json to delete multiple records
I am listing a table which shows records from the database and each row has a checkbox with an ID value associated with it.
Once the user has selected the checkboxes of the records they want to delete, the submit button is pressed and that triggers an ajax request.
That then goes to the controller:
Which is supposed to send the data to the model:
The error I am getting in console is:
undefined index: order_id
It is referring to this line in the model:
I am not sure at which point the problem is, is it in the data being sent via ajax, should the foreach loop be in the controller instead, or.....?
<label class="m-checkbox">
<input type="checkbox" name="order_id[]" value="<?php echo sanitize($failed->order_id); ?>">
<span></span>
</label>
Once the user has selected the checkboxes of the records they want to delete, the submit button is pressed and that triggers an ajax request.
$( '.delete-failed' ).on('click', function(e) {
e.preventDefault();
var form = $( '#delete-failed' ).serialize();
$.ajax({
url: url + '/TransactionsAjax/deleteFailedTransactions',
type: 'POST',
data: form,
dataType: 'json',
beforeSend: function() {
$( 'delete-failed' ).prop('disabled', true);
}
})
.done(function (data) {
if(!data.success) {
$( '.alert-danger' ).append(data.message).fadeIn();
} else {
$( '.alert-success' ).append(data.message).fadeIn();
}
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log(textStatus + ': ' + errorThrown);
console.warn(jqXHR.responseText);
});
});
That then goes to the controller:
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$response = array();
$message = '';
$orderId = $_POST['order_id'];
$data = [
'order_id' => $orderId
];
if($this->TransactionsModel->deleteFailedTransactions($data)) {
$response['success'] = true;
$response['message'] = 'Failed transactions deleted';
} else {
$response['success'] = false;
$response['message'] = 'Something went wrong. Please try again later.';
}
echo json_encode($response);
}
Which is supposed to send the data to the model:
public function deleteFailedTransactions($data)
{
$this->db->beginTransaction();
try {
$this->db->query("DELETE FROM `order_summary` WHERE `order_id` = :order_id");
$this->db->bind(":order_id", $order_id);
foreach($data as $item) {
$order_id = $item['order_id'];
$this->db->execute();
}
$this->db->query("DELETE FROM `order_detail` WHERE `order_id` = :order_id");
$this->db->bind(":order_id", $order_id);
foreach($data as $item) {
$order_id = $item['order_id'];
$this->db->execute();
}
$this->db->commit();
return true;
}
catch(Exception $e) {
$this->db->rollBack();
echo $e;
return false;
}
}
The error I am getting in console is:
undefined index: order_id
It is referring to this line in the model:
$order_id = $item['order_id'];
I am not sure at which point the problem is, is it in the data being sent via ajax, should the foreach loop be in the controller instead, or.....?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Haha - know what you mean :)
ASKER