Trying to pass selected rows to php
Trying to pass selected rows to php
rjpiston
Posts: 8Questions: 2Answers: 0
I have a datatable setup with select enabled. I can select a number of rows, and grab the data but how do I pass that through ajax into my php page? I'd like to loop through the array of selected rows and delete them.
function deleteNote(){
var dataToDelete = tblnotes.rows('.selected').data();
$.ajax({
url: '../admin/modules/projects/functions/deleteprojectnote.php',
dataType: 'json',
cache: false,
contentType: false,
processData: false,
data: dataToDelete,
type: 'post',
success: function(data){
if (!data.success) {
console.log("failed " + data.note_error);
}else{
console.log("succeeded " + data.message);
}
}
})
};
<?php
$errors = array();
$data = array();
$notesArray = $_REQUEST['dataToDelete'];
if (!empty($errors)) {
$data['success'] = false;
$data['errors'] = $errors;
$data['message'] = "Something went wrong, the project note could not be deleted.";
}else{
$data['success'] = true;
$data['message'] = "success" . $notesArray;
}
echo json_encode($data);
This discussion has been closed.
Answers
In line 9 (
data: dataToDelete
) you might need to use JSON.stringify() to encode the data into a JSON string.Kevin
I'm still getting this error:
Undefined index: dataToDelete
The line affected:
Likely its because you aren't sending an object with the key of
dataToDelete
. Maybe you need something like this:Since you are using jQuery ajax() to send the data you might check on Stack Overflow for help understanding how to send POST data to a PHP script.
Kevin
Ok thank you for the quick reply.
Is there a working example of some kind that you can point me towards? I'm really struggling here and stackoverflow wasn't much help