Get data on the remove row to perform added action

Get data on the remove row to perform added action

webpointzwebpointz Posts: 126Questions: 30Answers: 4

I have a editor datatable with a REMOVE button.

When I highlight the row and click REMOVE, I would like to update another table with the 'quantity' of the row being deleted but not sure how to get it because I receive an error "Notice: Undefined index: data".

The row I am removing has a quantity that I want to log to another table, so I need to get the "itemid" from the row being deleted so I can query the current table and get the quantity. I'm using $_POST['data']['table_productitems']['itemid'].

Here is my code in the PHP editor file:

    $sql = "SELECT quantity FROM table_productitems WHERE itemid = ".$_POST['data']['table_productitems']['itemid'];
    $result= $conn->query($sql);

    if ($result->num_rows > 0) {
         // output data of each row
         while($row = $result->fetch_assoc()) {
            $addStockCount = $row["quantity"];

         }
    }   

      $db->insert( 'table_productstock', [ 'productid' => $thisproduct, 'productname' => $thisproductname, 'insertdate' => date('Y-m-d H:i:s'), 'userid' => $_SESSION['userid'], 'customerid' => $_SESSION['w2'], 'stockcount' => $addStockCount ] );

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Answer ✓

    The preRemove event from the PHP libraries is the way to do this. It will give you the data for the row, and execute once per row being removed, and only when rows are being removed.

    Allan

  • webpointzwebpointz Posts: 126Questions: 30Answers: 4

    Thanks Allan

    That will work. Just a question...how far back in version numbers does preRemove and postRemove work?

  • webpointzwebpointz Posts: 126Questions: 30Answers: 4

    One more question, how is the data accessed from "values"?

    For instance, for "json_encode( $values )" how would I access the quantity value?

    {"lotnumber":"123456","expirydate":"2008-11-28","quantity":"12"}

  • webpointzwebpointz Posts: 126Questions: 30Answers: 4

    It's ok, I am doing the following and it's working:

        function logChangeCreate ( $db, $action, $id, $values ) {
    
        $json = json_encode( $values );
        $json = json_decode($json, true);
        $quantity = $json['quantity'];
    
            $db->insert( 'product-logs', array(
            'action' => $action,
            'rowvalues' => $json,
            'quantity'    => $quantity
        ) );
    }
    
    
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Editor 1.5.0 was the version that introduced server-side events. preRemove was in that version, although it was only made cancellable in 1.6 or 1.7 I think.

    Allan

This discussion has been closed.