Pass ajax parameter to where condition in php
Pass ajax parameter to where condition in php
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
See if using
ajax.data
as a function does what you want. There are examples in the docs.Kevin
I do not quite grasp the logic, sorry. How should I add it to my buttons? I create them like this:
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:Kevin
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
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.
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 theajax.data
option in the Editorsajax
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
@welleozean77 Maybe it's to late or you just got it working but literally just put this:
There:
And there: