How to pass a variable from php to the server-side editor

How to pass a variable from php to the server-side editor

ken@kmplace.comken@kmplace.com Posts: 5Questions: 4Answers: 0

I'm struggling to understand how to pass a variable from PHP from the client to the server editor. The following works if I hard code the value in. I'd like to pass in the value of a variable (ie. $territory) instead of the 'L-140'.

Can some one show me how this is done?

My code:

var table = $('#names').DataTable( {
    dom: 'Bfrtip',
    ajax: {
            url: '../assets/dt/php/table.names.php',
            data:   { 'territory': 'L-140' }                      <<--- would like a variable in stead of 'L-140'
        },
    type:   'POST',

    columnDefs: [
        { targets: [0, 2, 4, 5, 6], visible: false }
    ],  
    columns: [
        { "data": "names.con_id" },
        { "data": "names.territory" },
        { "data": "contacts.category" },
        { "data": "contacts.status" },
        { "data": "names.postal" },
        { "data": "names.street" },
        { "data": "names.house" },
        { "data": "names.first_name", className: 'editable' },
        { "data": "names.last_name", className: 'editable' },
        { "data": "contacts.fullstreet" },
        { "data": "postals.city" },
        { "data": "names.phone", className: 'editable' },
        { "data": "names.mobile", className: 'editable' },
        { "data": "names.email", className: 'editable' }
    ],
    select: true,
    ordering: false,
    lengthChange: false,
    buttons: [
        { extend: 'create', editor: editor },
        { extend: 'edit',   editor: editor },
        { extend: 'remove', editor: editor }
    ]
} );

} );

[Server side]

Editor::inst( $db, 'names', 'id' )
->fields(
Field::inst( 'names.con_id' ),
Field::inst( 'names.territory' ),
Field::inst( 'contacts.category' ),
Field::inst( 'contacts.status' ),
Field::inst( 'names.postal' ),
Field::inst( 'names.street' ),
Field::inst( 'names.house' ),
Field::inst( 'names.first_name' ),
Field::inst( 'names.last_name' ),
Field::inst( 'contacts.fullstreet' ),
Field::inst( 'postals.city' ),
Field::inst( 'names.phone' ),
Field::inst( 'names.mobile' ),
Field::inst( 'names.email' )
)
->leftJoin('contacts','contacts.id','=','names.con_id')
->leftJoin('postals','postals.postal','=','names.postal')
->where( 'names.territory', $_GET['territory'])
->process( $_POST )
->json();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin

    data: { 'territory': 'L-140' }

    Where is the value coming from? If you have it in a variable already then you can just use:

               data:   { 'territory': territory } 
    

    but I suspect the problem is more that you don't know how to get the value for the variable from wherever it is. But I'm not sure where it is I'm afraid? In the URL, or another object, localStorage, or something else?

    Thanks,
    Allan

  • Alexandr45Alexandr45 Posts: 30Questions: 1Answers: 3

    This is how it works for me.

    ajax: {
    url: '../assets/dt/php/table.names.php',
    data: function(d){
    d.territory = 'L-140';
    }
    }

    PHP:
    ->where( 'names.territory', $_GET['territory'])

  • ken@kmplace.comken@kmplace.com Posts: 5Questions: 4Answers: 0

    I should have been more clear. The variable exists in my php code as $territory.

    I assume that I need to get the php variable into the client side javascript and then send it as ajax data to the server side. Once there it will show up in either $_POST or $_GET.

    How do pass the php variable into the client side javascript?

    If I create a variable in the Javascript (ie. var territory) and assign a value to it, it works fine. Iā€™m drawing a blanks on getting the php variable into the client javascript.

    My knowledge of javascript is not to best.

  • Alexandr45Alexandr45 Posts: 30Questions: 1Answers: 3

    var territory = <?php echo $territory; ?>;

  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin
    Answer āœ“

    Small change to that:

    var territory = <?php echo json_encode( $territory ); ?>;
    

    Then you don't need to worry about quoting strings etc.

    Allan

This discussion has been closed.