$_POST data as session variable not working when using table.column syntax?!

$_POST data as session variable not working when using table.column syntax?!

nigel pasconigel pasco Posts: 37Questions: 6Answers: 0
edited August 2014 in Free community support

Following on from this discussion:
https://datatables.net/forums/discussion/20925/add-value-in-server-side-script

I have managed to replicate passing the session variable 'Owner' using:

$_POST['data']['Owner'] = $_SESSION['Owner'];

However, in my example, I also need to link to other tables, so I have tried setting the field name with table.column syntax:

$_POST['data']['users.Owner'] = $_SESSION['users.Owner'];

But the $_POST statement does not work when the table name is included. It only works when the table name is ommited.

Any idea why this is, or how to resolve this issue?

My example is below ( I tried to set the last_name field with a set variable $lname). The field names in the JS were changed to users.last_name also.

<?php

/*
 * Example PHP implementation used for the index.html example
 */

$lname = 'Smith';

// DataTables PHP library
include( "../../php/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Validate;

// Build our Editor instance and process the data coming from _POST
$editor = Editor::inst( $db, 'users' )
    ->fields(
        Field::inst( 'users.last_name' ),
        Field::inst( 'users.first_name' ),
        Field::inst( 'users.updated_date' )
            ->set( false )
            ->getFormatter( 'Format::date_sql_to_format', 'D, jS F Y' )
    );

if ( isset($_POST['action']) && ( $_POST['action'] === 'create' || $_POST['action'] === 'edit' ) ) {
    $_POST['data']['users.last_name'] = $lname;
};

$editor
    ->process( $_POST )
    ->json();

This question has an accepted answers - jump to answer

Answers

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

    But the $_POST statement does not work when the table name is included. It only works when the table name is omitted.

    As in the users. part of the code here: $_POST['data']['users.last_name'] = $lname;?

    So:

    $_POST['data']['users.last_name'] = $lname; // doesn't work
    $_POST['data']['last_name'] = $lname; // works
    

    Is that correct?

    Allan

  • nigel pasconigel pasco Posts: 37Questions: 6Answers: 0

    yes, alan. That seems to be the case.

  • nigel pasconigel pasco Posts: 37Questions: 6Answers: 0

    Hi Allan.
    I have put together a couple simple test case based on the tableOnlyData here:

    http://nigelpasco.com/datatables/extensions/Editor-1.3.2/examples/advanced/test.html

    In test.html, 'last_name' is referred to in all instances as 'last_name'. In the server script, I have a variable $lname="Smith"; and a $_POST function to overwrite the entered last name with $lname="Smith". This works fine.

    http://nigelpasco.com/datatables/extensions/Editor-1.3.2/examples/advanced/test2.html

    However, in test two, I have included the table reference to 'last_name' instances as 'users.last_name' to allow linking to other tables. Note that the $_POST function does not work, and the entered last name goes through.

    I hope these examples help you explore. The code should be on the page, as per your examples.

    Thank you for looking into it.
    Nige

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

    In test2 it doesn't appear to be using the join syntax. Should it be? It is submitting last_name as just that - not as another named parameter.

    One thing, when using the dotted object notation - for example users.last_name - the data is submitted in the form [users][last_name]. So in PHP you would access it using $_POST['data']['users']['last_name'].

    Allan

  • nigel pasconigel pasco Posts: 37Questions: 6Answers: 0

    Thank you Allan, the form worked correctly using the $_POST in the form:
    $_POST['data']['users']['last_name']

    An oversight on my part.

    Datatables editor is more impressive the further I delve into it.
    Cheers mate,
    nige

This discussion has been closed.