通过数据表ajaxsource发送变量

时间:2013-09-05 15:11:02

标签: php javascript jquery json datatables

Javascript,我正在尝试使用fnserverparams,但我可能做得不对。

$(document).ready(function () {

$('#dblist').on('change', function () {

    var selected = $("select option:selected").text();
    tablefill(selected);

});

$('#search').click(function () {

    var selected = $("select option:selected").text();
    tablefill(selected);

});

function tablefill(selected) {

    $('#table_id').dataTable({
        "sAjaxSource": '/php/connect/searchtablequery.php',
        "bProcessing": true,
        //"bServerSide": true,
        "bDestroy": true,
        "sAjaxDataProp": "",
        "fnServerParams": function (aoData) {
            aoData.push({ "db": selected });
        },
        "aoColumns": [
            { "mData": "calldate" },
            { "mData": "clid" },
            { "mData": "src" },
            { "mData": "dst" },
            { "mData": "dcontext"}],
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "sDom": '<"H"Tfr>t<"F"ip>',
        "oTableTools": {
            "sSwfPath": "/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf",
            "aButtons": [
                "copy", "csv", "xls", "pdf",
                {
                    "sExtends": "collection",
                    "sButtonText": "Save",
                    "aButtons": ["csv", "xls", "pdf"]
                }
            ]
        }
    });
};

});

我使用php来获取变量

<?php  
session_start();
include '../connect/adminconnect.php'; 

$myuserdb   = $_POST['db'];

$dbidsql = $dbh->prepare("SELECT * FROM DBList WHERE DBNick= :db"); 
$dbidsql->execute(array(':db' => $myuserdb));;  
$userdbid = $dbidsql->fetchAll();

$dbip   = $userdbid['DBIP'];
$dbuser = $userdbid['DBUser'];
$dbpass = $userdbid['DBPass'];
$dbname = $userdbid['DBName'];
$remotetblname="crd"; // Table name 

// Connect to server and select databse.
try {
    $remotedbh = new PDO("mysql:host=$dbip;dbname=$dbname", "$dbuser", "$dbpass");
}
catch(PDOException $e) {
    echo $e->getMessage();
}

$query = 'SELECT * FROM cdr'; 
$sql = $remotedbh->prepare($query);  
$sql->execute(); 
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC); 

if($sql->rowCount() > 0){ 
    header('Content-type: application/json');
    echo json_encode($dblist); 
} 
else {
    echo 0; 
}  

&GT;

我有json和一切工作我只是希望用户能够从下拉列表中选择要查询的数据库,这是有效但我无法弄清楚如何传递变量因为所有的dbip并且一切都是空的

1 个答案:

答案 0 :(得分:0)

据我所知,数据表检索数据的默认方法是GET,但您在db变量中寻找$_POST

此外,请确保使用数据表版本1.8.2或更新版本,因为该版本已引入fnServerParams,请参阅http://www.datatables.net/forums/discussion/6600/question-about-fnserverparams