简体   繁体   English

发送javascript / json变量到php sql请求

[英]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. 我正在使用带有serverSide选项的数据表,但我想将javascript变量传递到php变量中,以便以后在php sql select请求中使用它。 i need sending that var without refreshing the page. 我需要发送该var而不刷新页面。 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 使用$_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 ). 从当前页面(例如index.php ),发出一个jQuery $.ajax POST(或GET)请求,将JavaScript变量作为参数传递给另一个页面(例如processValue.php )。

(POST) Example: (POST)示例:

$.ajax({
  method: "POST",
  url: "processValue.php",
  data: { someVariable: someValue }
});
  1. In processValue.php , save the receiving param's value into PHP's $_SESSION variable processValue.php中 ,将接收参数的值保存到PHP的$_SESSION变量中

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 从这一点开始(即,根据请求成功完成),您的$_SESSION变量将具有index.php中可访问的值,您可以稍后对其进行处理

NOTE: 注意:

  1. index.php and processValue.php file names are used merely for example purposes... index.php和processValue.php文件名仅用于示例目的...

  2. Never access $_POST directly without sanitizing etc. --- this is shown merely for example purposes and to get you going! 切勿在未经消毒等情况下直接访问$ _POST。---此处显示的内容仅出于示例目的,并且可以助您一臂之力!

  3. In index.php and processValue.php , make sure you do session_start(); index.phpprocessValue.php中 ,确保执行session_start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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