简体   繁体   中英

send javascript/json variable to a php sql request

i am using datatables with serverSide option, but i want to pass a javascript variable into php variable to use it later on php sql select request. i need sending that var without refreshing the page. Thanks

 <script type="text/javascript"> $( document ).ready(function() { $('#employee_grid').DataTable({ "bProcessing": true, "serverSide": true, "bStateSave": true, "searching": true, "aaSorting": [[0,'desc']], "ajax":{ url :"response2.php", //json datasource type: "post", //type of method, by default would be get "aoColumnDefs" : [ { 'bSortable' : true, 'aTargets' : [1,2,3,4,5,6,7,8,9] }], "dataSrc": function (jsonData) { for ( var i=0, len=jsonData.data.length ; i<len ; i++ ) { jsonData.data[i][1] = '<font size="4px">'+jsonData.data[i][2]+'</font>'; <?php $phpvar=jsonData.data[i][2]; $sql="SELECT * FROM clientwhere id='$phpvar'"; ... ?> } return jsonData.data; }, error: function(){ // error handling code // $(".employee-grid-error").html(""); //$("#employee_grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>'); $("#employee_grid_processing").css("display","none"); } } }); }); </script> 

Use $_SESSION

  1. From your current page (eg index.php ), make a jQuery $.ajax POST (or GET) request, passing in the JavaScript variable as a param, to another page (eg processValue.php ).

(POST) Example:

$.ajax({
  method: "POST",
  url: "processValue.php",
  data: { someVariable: someValue }
});
  1. In processValue.php , save the receiving param's value into PHP's $_SESSION variable

Example:

$_SESSION["javascriptVariableValue"] = $_POST['someVariable'];
  1. From this point on (ie upon request completing successfully), your $_SESSION variable will have the value accessible in index.php that you can process later

NOTE:

  1. index.php and processValue.php file names are used merely for example purposes...

  2. Never access $_POST directly without sanitizing etc. --- this is shown merely for example purposes and to get you going!

  3. In index.php and processValue.php , make sure you do session_start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM