Create/Edit record using userid in PHP variable in DB column

Create/Edit record using userid in PHP variable in DB column

peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
edited November 2019 in Editor

I have DB columns 'updated_by' and 'created_by' where the DataTables userid (logged-in user) needs to be recorded automatically.

The userid is stored in a PHP variable $user

How can I put this variable value into those fields? The fields are not available in the editor, but show in DataTables.

Editor::inst( $db, 'user', 'user_pk' )
    ->fields(
    Field::inst( 'user_id' ),
    Field::inst( 'first_name' ),
    Field::inst( 'last_name' ),
    Field::inst( 'email' ),
    Field::inst( 'user_type' ),
    Field::inst( 'created' ),
    Field::inst( 'created_by' ),
    Field::inst( 'updated' ),
    Field::inst( 'updated_by' )
)->process( $_POST )->json();

This question has an accepted answers - jump to answer

Answers

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    OK, I have changed the fields a bit and hidden the renamed fields as 'modified' and 'modified_by':

    Still not updating the 'modified_by' column using the PHP variable captured via a hidden form element to a JQuery variable 'user_id'

    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    Editor::inst( $db, 'user', 'user_pk' )
        ->fields(
        Field::inst( 'user_id' ),
        Field::inst( 'first_name' ),
        Field::inst( 'last_name' ),
        Field::inst( 'email' ),
        Field::inst( 'user_type' ),
        Field::inst( 'modified' ),
        Field::inst( 'modified_by' )
    )->process( $_POST )->json();
    

    and using:

        <table id='user_table' class='display' style="width:100%">
                <thead>
                    <tr>
                        <th>User ID</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Email</th>
                        <th>User Type</th>
                        <th>Modified</th>
                        <th>Modified By</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
            <div id="user_form">
                <editor-field name="user_id"></editor-field>
                <editor-field name="first_name"></editor-field>
                <editor-field name="last_name"></editor-field>
                <editor-field name="email"></editor-field>
                <editor-field name="user_type"></editor-field>
                <editor-field name="modified"></editor-field>
                <editor-field name="modified_by"></editor-field>
            </div>
    
    var editor = new $.fn.dataTable.Editor( {
                    ajax: "program_data/user_data.php",
                    table: "#user_table",
                    template: '#user_form',
                    fields: [ {
                        label: "User ID:",
                        name: "user_id"
                    }, {
                        label: "First name:",
                        name: "first_name"
                    }, {
                        label: "Last name:",
                        name: "last_name"
                    }, {
                        label: "Email:",
                        name: "email"
                    }, {
                        label: "User Type:",
                        name: "user_type",
                        type: "select",
                        options: [
                            "read",
                            "edit",
                            "create",
                            "super"
                        ]
                    }, {
                        type:'hidden',
                        name: "modified"
                    }, {
                        type:'hidden',
                        name: "modified_by",
                        def: user_id
                    } ]
                } );
    
                $( '#user_table' ).DataTable( {
                    ajax: "program_data/user_data.php",
                    dom: "Bfrtip",
                    columns: [ {
                        data: "user_id"
                    }, {
                        data: "first_name"
                    }, {
                        data: "last_name"
                    }, {
                        data: "email"
                    }, {
                        data: "user_type"
                    }, {
                        data: "modified"
                    }, {
                        data: "modified_by"
                    } ],
                    select: {
                        style: 'os',
                        selector: 'td:first-child'
                    },
                    buttons: [ {
                        extend: "create",
                        editor: editor
                    }, {
                        extend: "edit",
                        editor: editor
                    }, {
                        extend: "remove",
                        editor: editor
                    } ]
                } );
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    You can use Field->setValue() for this - e.g.:

    Field::inst( 'modified_by' )->setValue( $_SESSION['user_id'] )
    

    Full API reference docs for PHP are available here.

    Allan

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    Thanks again Allan! Also I needed to add session_start(); at the beginning of the server PHP editor script:

    <?php
    session_start();
    include( "../datatables/lib/DataTables.php" );
    // Alias Editor classes so they are easy to use
    
    date_default_timezone_set('Australia/Perth');
    
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    Editor::inst( $db, 'user', 'user_pk' )
        ->fields(
        Field::inst( 'user_id' ),
        Field::inst( 'first_name' ),
        Field::inst( 'last_name' ),
        Field::inst( 'email' ),
        Field::inst( 'user_type' ),
        Field::inst( 'modified' ),
        Field::inst( 'modified_by' )->setValue( $_SESSION['cm_user'] )
    )->process( $_POST )->json();
    ?>
    
This discussion has been closed.