How do I pass a hidden value (user-id) to Ajax POST? (a newbie question)

How do I pass a hidden value (user-id) to Ajax POST? (a newbie question)

predicapredica Posts: 3Questions: 1Answers: 0
edited June 2016 in Free community support

Hi,

I am a relative newbie to Datatables. I have been using it for a couple of weeks and it's going well but I am completely stumped on how to pass a hidden user_id value through a Create form to my server. I have researched this extensively over the past few days and spent a long amount of time on it but I'm completely stuck.

Any help here would be massively appreciated as this is a fantastic product which I'm keen to use and learn more about.

I've simplified my code here by only including 1 field in the table ("record_name") and the "user_id" field which is hidden and which I wish to POST through the Create form.

In my JS I am trying to post the value of 5 for the "user_id" field

Thanks in advance


Installation: ("records.php") <?php session_start(); /* * Example PHP implementation used for the index.html example */ // DataTables PHP library include( "DataTables.php" ); // Alias Editor classes so they are easy to use use DataTables\Editor, DataTables\Editor\Field, DataTables\Editor\Format, DataTables\Editor\Mjoin, DataTables\Editor\Upload, DataTables\Editor\Validate; Editor::inst( $db, 'tbl_xxxxx') ->field( Field::inst('tbl_xxxxx.record_name'), Field::inst('tbl_xxxxx.user_id') ) ->process( $_POST ) ->json(); <?php > ?>
JS:

var editor; // use a global for the submit and return data rendering in the examples


$(document).ready(function() {


  editor = new $.fn.dataTable.Editor( {



    "ajax": "php/records.php",
    "table": "#example", 
    "fields": [ 

    {
        "label": "Record_name",
      "name": "tbl_xxxxx.record_name",
        "placeholder": "",
        "def": ""
    },
 {
        "label": "user_id",
      "name": "tbl_xxxxx.user_id",
        "type": "hidden"
       
    }   
              ]

    } 
    );



   
  $('#example').DataTable( {

    retrieve: true,
    /*paging: false,*/
    dom: "Bfrtip",

    ajax: {
      url: "php/records.php",
         type: "POST",
   "data": {"user_id":"5"}
           },
                         

    "select": true,
      "processing": true,
          "serverSide": true,
          "bLengthChange": false,
          "bScrollCollapse": true,
        columns: [
       
   { data: "tbl_xxxxx.record_name"}
                ],


  select: true,
        buttons: [
            { extend: "create", editor: editor }/*,
            //*{ extend: "edit",   editor: editor },
            //{ extend: "remove", editor: editor }*/
        ],
       "oLanguage": {"sZeroRecords": "You are do not have any pegs yet. Click the <b>New</b> button on the left to start adding pegs", "sEmptyTable": " You have no alerts currently"}
    } );
} );

This question has an accepted answers - jump to answer

Answers

  • predicapredica Posts: 3Questions: 1Answers: 0

    Sorry for the lack of formatting in the code

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26
    Answer ✓

    If you simply want the default for user_id to be 5 you can use the fields.def to define the default value for a new record.
    That value can then be dynamic, alternatively you could store the user_id in a session and not involve the client-side in anyway.

    To set values for a field on the server-side use a setValue function.

    Field::inst('myField')->setValue( $_SESSION['user_id'] )
    
  • predicapredica Posts: 3Questions: 1Answers: 0

    Hi Tom,

    I have just tested the setValue suggestion and it worked. Result! Thanks a million. This was one of the things I did look at but I was passing the session var in double quotes. Sorry, a newbie mistake

    You've saved me a ton of time - much appreciated!

This discussion has been closed.