title: How do you dynamically set ajax data in DataTables?
body: <p>I'm using DataTables 1.10.15 and have read about how you can specify the <code>data</code> attribute in an ajax call like so:</p>
  
  <pre><code>$(document).ready(function() {
      var MyTable = $('#example').DataTable( {
          "processing": true,
          "serverSide": true,
          "ajax": {
              "url": "scripts/server_processing.php",
              "data": function ( d ) {
                  d.myKey = "myValue";
                  // d.custom = $('#myInput').val();
                  // etc
              }
          }
      } );
  } );
  </code></pre>
  
  <p>However, this example just has hardcoded values (or form input values like <code>$('#myInput').val()</code>).</p>
  
  <p>I have an application where I need to be able to pass in various different objects of data and then have the ajax call run. </p>
  
  <p>I can't see how this is possible and have looked at the following <a href="https://datatables.net/examples/server_side/custom_vars.html" rel="nofollow noreferrer">https://datatables.net/examples/server_side/custom_vars.html</a>.</p>
  
  <p>The reason I want to do this is because I'm building an application with <b>several different forms</b>. I need to choose <b>which form</b> to send as the data! When the users enter terms in a particular form, I want to pass <em>that</em> forms data to my ajax script and then have DataTables redraw the table (which I can do using the <code>.draw()</code> method):</p>
  
  <pre><code>MyTable.draw();
  </code></pre>
  
  <p>However, I don't understand how I can dynamically specify what goes in <code>data:</code>. I <em>could</em> do something like this in the ajax call:</p>
  
  <pre><code>"ajax": { 
          "url" : "scripts/server_processing.php",
          "data" : function ( d ) {
              d.primarySearch = $('#form1').serialize(),
              d.secondSearch = $('#form2').serialize(),
              d.thirdSearch = $('#form3').serialize(),
          }
      }
  </code></pre>
  
  <p>But, if the data I'm doing the search on is in <code>#form1</code>, I just want to pass that data, and not the data from <code>#form2</code> and <code>#form3</code>. But since that varies each time, I need to be able say which form(s) to pass in to the <code>data:</code> object.</p>