How do I send/perform additional data on server-side (PHP) with Editor?

How do I send/perform additional data on server-side (PHP) with Editor?

pansengtatpansengtat Posts: 66Questions: 26Answers: 1
edited July 2014 in Editor

Hi,

I recently posted a question related to this topic on the Free Forum at (https://datatables.net/forums/discussion/22288/how-do-i-save-encoded-password-entries-when-using-datatables-editor#latest), but to little response. Now I am rephrasing the question so that I can drill to specific problems and then hope to get specific responses to these questions I raised.

The first thing I noticed about using Editor for DataTables is that only fields present in the Editor frame could send the data to the database. So my 1st question is, how do I create a hidden field within that Editor frame to store a hidden variable so that when the user clicks Create or Update in that Editor frame, all visible and hidden field data can be sent to the database?

Another thing that I wish to do is that just before all data from Editor is written into the database's table, I would like to intercept at least 1 field from Editor (eg. password) and modify the data (i.e. hash it) and then write that modified data into the DB. The problem now is that with Editor/Datatables, even though I specify "type":"password" for the Password field, the data appearing in the DB is not encrypted. How do I intercept a field from Editor just before saving it to the DB?

The last thing is that I would like to write additional data into the DB from a PHP script, so how do I signal the Editor.php/Editor.js files to get that additional data from an external PHP script, then send both Editor fields and extra data (processed by external PHP script) to the DB?

[Code can be found in the URL: https://datatables.net/forums/discussion/22288/how-do-i-save-encoded-password-entries-when-using-datatables-editor]

Replies

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

    So my 1st question is, how do I create a hidden field within that Editor frame [...]

    Use the hidden field type :-). Documentation for it is available here: http://editor.datatables.net/reference/field/hidden .

    How do I intercept a field from Editor just before saving it to the DB?

    Do you want to do that in PHP on the server-side? If so, use:

    $postData = $_POST;
    
    if ( isset( $postData['action'] ) && ( $postData['action'] === 'create' || $postData['action'] === 'edit' ) ) {
       $postData['data']['password'] = sha1( $postData['data']['password'] );
    }
    

    changing the hashing function and password parameter as you need of course.

    so how do I signal the Editor.php/Editor.js files to get that additional data from an external PHP script, then send both Editor fields and extra data (processed by external PHP script) to the DB?

    You can get extra data in the data request using the same method that is in the Join examples for populating the select lists: http://editor.datatables.net/examples/simple/join.html (click on the Server script tab and look at line 30).

    To send extra data, you can either use hidden fields, preSubmit, ajax.data as you require.

    Allan

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

    Update for anyone that finds this post, you may wish to use a set formatter to password encode:

        Field::inst( ... )
            ->setFormatter( function ( $val ) {
                return sha1( $val );
            } )
    

    Allan

  • schiefersoftschiefersoft Posts: 7Questions: 2Answers: 2
    edited November 2015

    OK but the better way is to do that on Client side!
    How can i replace the original password on client with the decoded password and send only the decoded to the server?

    what I need: how can I do that on client:

    Field::inst( ... )
    
        ->setFormatter( function ( $val ) {
    
            return sha1( $val );
    
        } )
    

    ?

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

    You'd need a crytpo library in Javascript that can create SHA1 hashes - for example you might use crypto-js.

    Allan

  • schiefersoftschiefersoft Posts: 7Questions: 2Answers: 2
    edited November 2015

    OK crypto was not the problem I have found the solution:
    I update the field in the

    editor.on('preSubmit', function (e, o, action)
    ...
    o.data[0].users.password = hex_sha512(password);
    ...
    

    works fine for me

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

    Are you using something like this for hex_sha512?

    One thing, you might want to use:

    $.each( o.data, function ( value, key ) {
     ...
    } );
    

    since multiple could be updated at a single time (unless you are using an old version of Editor).

    Allan

This discussion has been closed.