Pass ajax parameter to where condition in php

Pass ajax parameter to where condition in php

welleozean77welleozean77 Posts: 7Questions: 3Answers: 0
edited May 2020 in Free community support

I want to select a subset of a database table and display this in Datatable Editor.

This is easily done by adding a statement like the following in the php file and by passing my string (token) from the main HTML page:

Editor::inst( $db, 'Data', 'id' )
    ->fields(
        Field::inst( 'Term1' ),
        Field::inst( 'Term2' ),
        Field::inst( 'Term3' ),
        Field::inst( 'Tag' )
    )
    ->where( 'Tag', `$_REQUEST['token'] )
    ->process( $_POST )
    ->json();

Where in the HTML page I have:

$('#example').DataTable( {
      "ajax": {
        "url": "../../editorSQLite/php/table.$token.php",
        "data": {"token": 3}
         },
...

This works fine for loading the subset of the db table. However, the value 'token' is not passed by the three buttons "New, Edit, Delete"! Datatable complains with the message "A system error has occurred" (detailed error: "Notice: Undefined index: token in /www/htdocs/w0170926/mydomain.eu/editorSQLite/php/table.q7TABSAR.php on line 56 {"data":[]}"). PS: if I use a string instead of `$_REQUEST['token'], the buttons work fine.

Is there a way to fix my issue and pass a value to the php not only when I load the data the first time (which works perfectly), but also when the user uses the three buttons to edit the table?

Answers

  • kthorngrenkthorngren Posts: 21,181Questions: 26Answers: 4,924

    See if using ajax.data as a function does what you want. There are examples in the docs.

    Kevin

  • welleozean77welleozean77 Posts: 7Questions: 3Answers: 0

    I do not quite grasp the logic, sorry. How should I add it to my buttons? I create them like this:

    new $.fn.dataTable.Buttons( table, [
            { extend: "create", editor: editor },
            { extend: "edit",   editor: editor },
            { extend: "remove", editor: editor }
        ] );
    
  • kthorngrenkthorngren Posts: 21,181Questions: 26Answers: 4,924

    Sounds like you want to pass a dynamic value in the Ajax request. You can use ajax.dataSrc as a function to do so. Maybe this will do what you want:

    $('#example').DataTable( {
          "ajax": {
            "url": "../../editorSQLite/php/table.$token.php",
            "data": function ( d ) {
                d.token = 3;
             }
             },
    ...
    

    Kevin

  • kthorngrenkthorngren Posts: 21,181Questions: 26Answers: 4,924

    Just realized you were asking about using the Editor buttons. You would do something similar in the Editor's ajax.data option to add additional values to pass in the request.

    Kevin

  • welleozean77welleozean77 Posts: 7Questions: 3Answers: 0

    Thank you Kevin, but just pointing to the fact that I need to use ajax.data does not help. I am aware I need to use it in some way, however my question is HOW. I do not see any (to me) clear example on how to add add an ajax call to the single buttons.

  • kthorngrenkthorngren Posts: 21,181Questions: 26Answers: 4,924

    I'm not clear on what exactly you are expecting. I'm assuming you want to pass the token along with the Editor operation (edit, create, remove) that normally happens. To do this you will use the ajax.data option in the Editors ajax config. I'm not sure where you will get the token value.

    However if you want to create a button that sends an Ajax request you can use this example as a starter. Then you would use jQuery ajax() to send the request.

    If this doesn't help then please provide detailed steps of what you are trying to accomplish.

    Kevin

  • ChrisKolliChrisKolli Posts: 28Questions: 8Answers: 0

    @welleozean77 Maybe it's to late or you just got it working but literally just put this:

                ajax: {
                    url: 'php/YourLinkGoesHere.php',
                    type: 'POST',
                    data:function ( d ) {
                        d.Token = '3';
                     }
                },
    

    There:

    var editor = new $.fn.dataTable.Editor( {
                ajax: {
                    url: 'php/YourLinkGoesHere.php',
                    type: 'POST',
                    data:function ( d ) {
                        d.Token = '3';
                     }
                },
                table: ,
                fields: [
                    {},
                    {},
                ]
            } );
    

    And there:

    var table = $('#calendar').DataTable( {
                dom: 'Brtf',
                ajax: {
                    url: 'php/YourLinkGoesHere.php',
                    type: 'POST',
                    data:function ( d ) {
                        d.Token = '3';
                     }
                },
                columnDefs:[
                    {},
                ],
                order: [],
                columns: [
                    {}
                    {},
                ],
                select: true,
                lengthChange: false,
                buttons: [
                    { extend: 'create', editor: editor },
                    { extend: 'edit',   editor: editor },
                    { extend: 'remove', editor: editor },
                ],
    
This discussion has been closed.