Server side passing parameters
Server side passing parameters
kali83
Posts: 18Questions: 0Answers: 0
Hi,
i would need to pass 15 parameters to a php api.
But in the examples i don't see how it is done.
Example of ajax call in jquery:
$(document).ready(function() {
$("form#form1").submit(function(){
var var1= $('input#val1').val();
var var2 = $('input#val2').val();
$.ajax({
type: "POST",
url: "api.php",
data: {"va1": var1, "var2": var2},
dataType: "html",
contentType: 'application/x-www-form-urlencoded',
timeout: 3000
success: function(response) {
$("div#response").html(response);
},
error: function(){
alert("Call failed...");
}
});
});
});
How do i send the data?
Thank you
This discussion has been closed.
Replies
Take a look at the examples in the
ajax.data
docs. You will want to create a function to allow for sending updated data.Kevin
So... i can make a call even when changing a selectbox!
So i have to create a json file in the server and with an echo return javascript datatable with url name.json and the table header.
Quite right?
Thank you!
I'm not actually fully understanding your initial question I'm afraid. There isn't anything in the code there that relates to DataTables. It is a jQuery Ajax call that submits a form. Are you wanting to use those parameters in the Ajax fetch that DataTables is making? In which case, Kevin is spot on - use
ajax.data
.But it isn't clear to me that is actually what you are trying to do here. Perhaps you could explain what your objective is?
Allan
Of the examples i see, i must first create a json file, then run a js file and in the html i must always have the table header structure.
I should load at each change of a selectbox the data that are in a db and display them to the user.
is it possible and do it without going to create the json file? I return a json from php. At the end of the API i execute an "echo json_encode $table", without creating a json file on the server.
I hope, i have explained myself
Thank you!
Sure - just do that. DataTables' doesn't "care" if the original source is a static file or a PHP script - as long as it gets JSON in return.
Allan
I could write like that:
$(document).ready(function() {
$('form#form1').change(function(){
var var1 = $('input#var1').val();
var var2 = $('input#var2').val();
} );
Thank you!
$json = json_encode($tabDTSA);
$javaScript = '
<script src="../js/table.sa.js"></script>
<script src="../js/dt.fixedheader.mini.js"></script>
<script>
$(document).ready(function() {
var table = $("#tablesa").DataTable( {
fixedHeader: true,
"ajax": '.$json.',
"contentType": "application/json",
"columns": [
{
"className": "details-control",
"orderable": false,
"data": null,
"defaultContent": ""
},
} );</script>';
echo '
'
.$javaScript;
I get this error: DataTables warning: table id=tablesa - Ajax error. For more information about this error, please see http://datatables.net/tn/7
This error from chrome developer tool:
http://localhost/DataTable/test/?0%5B...........................(%22%23ckall_10595832870875f96c263c8343%22).click(function()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24(%22input%5Bname%3Dck_10595832870875f96c263c8343%22).prop(%22checked%22%2C%20%24(this).prop(%22checked%22))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24(%22input%5Bname%3Dck_10595832870875f96c263c8343%22).click(function()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(!%24(this).prop(%22checked%22))%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24(%22%23selectAll%22).prop(%22checked%22%2C%20false)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%3C%2Fscript%3E&_=1603715322108 414 (Request-URI Too Long)
The data I am going to return are many.
Is it possible that it fails to process because the string is too long?
Thank you!
Change the Ajax request to type
POST
so the parameters are sent in the payload and not on the URL.Kevin
I modified:
"ajax": {
"url": "'.$pathJson.'",
"type": "POST"
},
But i get the error: 405 not allowed nginx
If i insert the string json in the url it returns me not found.
if i insert the path of the json file it works but without "type": "POST"
Sorry... you could give me an example please!
Thank you!
Thats a server error outside of Datatables. Maybe this SO thread will help.
Kevin
I love you.... it works...
I have to understand how i can don't create the json file but directly from the decoded string.
if I write the string in the url json it returns jq.mini.js: 1 POST http://localhosta/ test /[object% 20Object] 404 (Not Found)
If i insert the object in the url it works.
For now it's okay...
Then as soon as i have some time i will return....
Thank you!