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;
}
?>
我有 json 并且一切正常我只希望用户能够从下拉列表中选择要查询的数据库,这正在工作,但我无法弄清楚如何传递变量,因为所有的 dbip 并且一切都是空的。