Using aoData to push extra things to a php file
Using aoData to push extra things to a php file
Hi, I am trying to use aoData.push to send extra variables to an external php file when making ajax calls (using POST data). I am wondering a few things-
1. Is this what aoData is actually doing? (i.e. can the php file use the aoData?)
2. How do i reference the added stuff? If my aoData.push is ..."name": "thing", "value": "thingsvalue"..., do i just say $_POST[thing] to get thingsvalue in the php file?
Here is my code for reference:
[code]
$(document).ready(function() {
$('#example').dataTable( {
"sAjaxSource": "finditems_post.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "blah", "value": "blahblah", "name": "thing", "value": "thingsvalue", } );
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
}
} );
} );
[/code]
Thanks, and this is an awesome framework!
1. Is this what aoData is actually doing? (i.e. can the php file use the aoData?)
2. How do i reference the added stuff? If my aoData.push is ..."name": "thing", "value": "thingsvalue"..., do i just say $_POST[thing] to get thingsvalue in the php file?
Here is my code for reference:
[code]
$(document).ready(function() {
$('#example').dataTable( {
"sAjaxSource": "finditems_post.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "blah", "value": "blahblah", "name": "thing", "value": "thingsvalue", } );
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
}
} );
} );
[/code]
Thanks, and this is an awesome framework!
This discussion has been closed.
Replies
2. You have "name" and "value" parameters defined twice in the same object... Try:
[code]
aoData.push( { "name": "blah", "value": "blahblah" } );
aoData.push( { "name": "thing", "value": "thingsvalue" } );
[/code]
Allan
Thanks for the reply. I'm still stuck- i try to define a variable in my php file to hold the value of one of the key value pairs that was pushed into aoData, and it seems like it isnt getting the pushed post data. For instance, if we pushed the data from your example above and then i tried the following in the php file,
$variable = $_POST['thing']
it ends up that $variable is empty, instead of thingsvalue, which is what i want it to hold. I am a newbie, so maybe there is a fundamental problem that i am overlooking?
Anyone ever got the Firebug error "aoData is null"?
I am trying something similar to http://datatables.net/forums/comments.php?DiscussionID=175.
My initialization works but I get the above error when I invoke "proTable.fnReloadAjax();"
Here is the relevant datatable init:
[code]
sAJx = '../../brofile/';
var proTable = $('#profiles').dataTable( {
"bPaginate": true,
"bLengthChange": false,
"bJQueryUI": true,
"bServerSide": true,
"bProcessing": true,
"bAutoWidth": true,
"bFilter": true,
"bSort": true,
/* AJAX SOURCE */ "sAjaxSource": sAJx,
"fnServerData": function ( sSource, aoData, rfrshCallbk ) {
aoData.push( { "name": "where_select_box", "value": $("#select_box option:selected").val() } );
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": rfrshCallbk
} );
},
"aoColumns": [ ...
[/code]
The truly odd thing is that if I look at the POST array via FireBug following the instantiation / first call of my datatable, then I see this as the last element in the array:
where_select_box Item 2
...so obviously things are working during init.
It only throws the error "aoData is null" on the "fnReloadAjax" call when I change the dropdown list.
Thanks for any help, whether from Alan or Anyone Else.
Shameless
As the documentation for the fnReloadAjax plug-in says, it is not suitable for use with server-side processing. To reload the data when using server-side processing, just call fnDraw. That might well fix the issue for you. Maybe... :-)
Allan
If you ever have time, would you mind explaining what the difference is between the server-side processing and what the guy is doing at http://datatables.net/forums/comments.php?DiscussionID=175.?
After reading your correction, I even went back and read the orientation documentation and now I am even more confused as to under what circumstances one would use fnReloadAjax . An explanation is not necessary to solve my problem, merely to orient.
Thanks for the prompt reply!
Shameless
heh - not what the documentation is supposed to achieve... :-). Server-side processing and "Ajax sourced" data are different in that DataTables does not retain any information between draws about the data (for every draw of the table it requests the information to be displayed from the server). The Ajax sourced data however will request all of the data to be displayed in a single XHR, thus allowing DataTables to do the data processing on the client-side.
The term "Ajax source" is confusing since the server-side processing option is actually using an XHR to get data - but I couldn't think of a better term for getting the entire data set through Ajax - answers on a postcard...
As such, fnReloadAjax will reload the entire data set, where as fnDraw for server-side processing will only get the currently displayed data (since that is all that is needed). The post you point out is actually slightly wrong in it's use of fnReloadAjax - as noted, it shouldn't be used with server-side processing and could be harmful to the data set...
Hope that helps a bit!
Allan
-medswa
see line 08 for the added parameter. replace these with your name and value. you can duplicate this line to add more name=value pairs.
http://www.datatables.net/release-datatables/examples/server_side/custom_vars.html
[code]
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/server_processing.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
/* Add some extra data to the sender */
aoData.push( { "name": "more_data", "value": "my_value" } );
$.getJSON( sSource, aoData, function (json) {
/* Do whatever additional processing you want on the callback, then tell DataTables */
fnCallback(json)
} );
}
} );
} );
[/code]
Obviously you need to add some code in your server side script to use the parameters you added to aoData.
[code]
<?php
$more_data = "";
if (isset($_GET['more_data']) $more_data = $_GET['more_data']; // use $_POST if you called $.getJSON with POST method.
// if you are passing this value into a database, make sure to sanitize it
//$more_data = mysql_real_escape_string($more_data);
?>
[/code]