Trying to pass selected rows to php

Trying to pass selected rows to php

rjpistonrjpiston 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);

Answers

  • kthorngrenkthorngren Posts: 20,315Questions: 26Answers: 4,771

    In line 9 ( data: dataToDelete ) you might need to use JSON.stringify() to encode the data into a JSON string.

    Kevin

  • rjpistonrjpiston Posts: 8Questions: 2Answers: 0

    I'm still getting this error:

    Undefined index: dataToDelete

    The line affected:

     $notesArray = $_REQUEST['dataToDelete'];
    
  • kthorngrenkthorngren Posts: 20,315Questions: 26Answers: 4,771

    Undefined index: dataToDelete

    Likely its because you aren't sending an object with the key of dataToDelete. Maybe you need something like this:

    var dataToDelete = {'dataToDelete': tblnotes.rows('.selected').data()};
    

    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

  • rjpistonrjpiston Posts: 8Questions: 2Answers: 0

    Ok thank you for the quick reply.

  • rjpistonrjpiston Posts: 8Questions: 2Answers: 0

    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

This discussion has been closed.