dataTable:将额外数据传递给php服务器端脚本

时间:2015-05-22 21:13:07

标签: javascript php json datatables

我需要更改sql语句中的“where-clause”,该语句用于从数据库中获取数据,并通过更改myInput的值并将其传递到服务器端,将其显示在dataTables中。我有这段代码:

<input id=myInput value="london">

<script>
$(document).ready(function(){
$('#example').dataTable({
    "processing": true,
    "serverSide": true,
    "ajax": {
        "url": "server_processing.php",
        "data": function ( d ) {
            d.myInput = $('#myInput').val();
        }
    }
    });
} );
</script>

服务器端有这段代码:

<?php
//...HERE I have defined these: $sql_details, $table, $primaryKey, $columns 

$where="`office`='$_POST['myInput']'";

require( 'ssp.class.php' );
echo    json_encode(SSP::simple($_GET,$sql_details,$table,$primaryKey,$columns,$where)); 

我哪里错了?我已经尝试过这个基于很多例子并在互联网上发帖但我最终得到的结果相同。

1 个答案:

答案 0 :(得分:0)

您的PHP脚本存在一些问题。

  1. 您使用$_POST['myInput']中没有大括号的引用键 语法。
  2. 您没有逃避$_POST['myInput']邀请进行SQL注入攻击。
  3. SSP::simple不接受包含WHERE子句的参数,您需要使用SSP::complex代替。
  4. 更正后的代码如下所示:

    <?php
    //...HERE I have defined these: $sql_details, $table, $primaryKey, $columns 
    
    require( 'ssp.class.php' );
    
    $conn = SSP::db($sql_details);
    
    $where="office=".$conn->quote($_POST['myInput']);
    
    echo json_encode(SSP::complex($_GET,$conn,$table,$primaryKey,$columns,$where));
    
相关问题