Retrieving the mysql ID for a newly created row.

Retrieving the mysql ID for a newly created row.

sohovfxsohovfx Posts: 4Questions: 0Answers: 0

Hey All,

How would I be able to pass the mysql ID (Auto-Incrementing Primary Key value) created after DataTables Editor sends a new row to the database?

I have a 'submitSuccess' event which will call a subsequent AJAX function to insert a record into a linker table with the newly created ID from when the Editor creates the record, but I have no idea how to retrieve the ID value.

Any help would be greatly appreciated! I haven't been able to find any help online after hours of searching.

Thanks!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    The best way to do it is to have the server return the data for the row that has been created in the row object - that would include the row id (and everything else for the row). In this way you can handle cases such as the database using a default value for fields, etc.

    The client / server communication method that Editor uses is documented here, in the manual. There are some examples in the latter part of the page that might be of interest to you, particularly the create example.

    Allan

  • sohovfxsohovfx Posts: 4Questions: 0Answers: 0
    edited February 2015

    Thanks for the response. I was reading through the document you linked to and had a question. How do I parse through the data array that gets passed to a preSubmit function to find the row id of the table row to be deleted?

    Consider the following:

    // creating an editor instance:
    
    editor = new $.fn.dataTable.Editor( {
    
      ajax: "./php/project.php",
    
      table: "#project",
    
      idSrc: "id",   //  <-------- creating the idSrc parameter mentioned in the link you posted
    
      fields: [ {
    
        default: "0",
    
        id: "id",
    
        label: "ID:",
    
        name: "id",
    
        type: "readonly"
    
      }, {
    
        id: "name",
    
        label: "Name:",
    
        name: "name"
    
      },
    
    
    // ... etc ...
    
    } );
    
    // creating a preSubmit function and checking if row is being editted or deleted
    
    editor.on( 'preSubmit', function ( e, data, action ) {
    
      if (action == "edit") {
    
        id = $('#id').val();
    
        xmlHttp.open("GET", "php/ajax.php?action=preEditProject&id="+id, true);
    
      }
    
      else if (action == "remove") {
    
        
    // ----->  now how do I retrieve the row id from the "data" variable passed to the function??
    
    // for example:
    
    // id = data[0].idSrc;  OR  id = data.idSrc;  (these do not work)
    
    // what is the proper syntax to assign the  id of the row to be deleted to a variable??
    
       xmlHttp.open("GET", "php/ajax.php?action=deleteProject&id="+id, true);
    
      }
    
      xmlHttp.onreadystatechange = refresh;
    
      xmlHttp.send(null);
    
    } );
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    In preSubmit the row id will be in the id property of the data object passed in.

    This is the documentation that describes the format of the data Editor builds for submission.

    Allan

  • sohovfxsohovfx Posts: 4Questions: 0Answers: 0
    edited February 2015

    Yes, but what is the code to read the property from the data object? The documentation does not have any coding examples. How would I reference the id from the data object defined in the code I'm using above?

    When I do:

    alert(JSON.stringify(data));

    I can see the elements in the array printed; it shows the action and the ID of the row to delete, but how do I print a specific element value? For instance:

    alert(data[0]);

    returns "undefined"

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Simply data.id should give you the id.

    You could console.log( data ) to see the data structure in your browser's console if you want to inspect it that way.

    alert(data[0]);

    That would return undefined since data is an object, not an array.

    Allan

  • sohovfxsohovfx Posts: 4Questions: 0Answers: 0

    Thank you!! ugghh, I can't believe I didn't try that.. : )

This discussion has been closed.