使用.getJSON传递变量用于条件SQL查询

时间:2015-10-30 17:34:00

标签: javascript php json ajax

我有一个生成表格的页面,其内容将使用.getJSON进行检索。但是我有一个要为该条件查询传递的变量。这是我的代码,

<?php $cust_id = $_GET['cust_id']; ?>
<table class="display" id="invoice-disc-list" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>INVOICE #</th>
            <th>INVOICE DATE</th>
            <th>SALESMAN</th>
            <th>INVOICE AMOUNT</th>
            <th>DISCOUNT</th>
        </tr>
    </thead>
</table>

<script>
    $.getJSON('pages/lj_menu/monitoring/process/price_history_data.php', function(response) {
        var custid = '<?php echo $cust_id; ?>';
        // Initialize DataTables
        $('#invoice-disc-list').DataTable({
            iDisplayLength : 15,
            processing: true,
            data: response,
            columns: [
              {data: "INVOICE_NO"},
              {data: "INVOICE_DATE"},
              {data: "INVOICE_SALESMAN"},
              {data: "INVOICE_AMOUNT"},
              {data: "INVOICE_DISCOUNT"}
          ]
        });

        // Initialize AJAX onClick Data Send
        window.someGlobalOrWhatever = response.balance;
    });
</script>

如何将var custid = '<?php echo $cust_id; ?>';发布到包含查询的price_history_data.php,以便表格中的数据仅显示基于我发送的custid变量的数据。?

再次感谢您的帮助

2 个答案:

答案 0 :(得分:1)

你在这里:

$.getJSON('pages/lj_menu/monitoring/process/price_history_data.php', '<?php echo $cust_id; ?>')
    .done(function(response) {
        // Initialize DataTables
        $('#invoice-disc-list').DataTable({
            iDisplayLength : 15,
            processing: true,
            data: response,
            columns: [
              {data: "INVOICE_NO"},
              {data: "INVOICE_DATE"},
              {data: "INVOICE_SALESMAN"},
              {data: "INVOICE_AMOUNT"},
              {data: "INVOICE_DISCOUNT"}
          ]
        });

        // Initialize AJAX onClick Data Send
        window.someGlobalOrWhatever = response.balance;
    });

答案 1 :(得分:1)

custid作为网址

中的get参数传递
<?php $cust_id = $_GET['cust_id']; ?>
<table class="display" id="invoice-disc-list" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>INVOICE #</th>
            <th>INVOICE DATE</th>
            <th>SALESMAN</th>
            <th>INVOICE AMOUNT</th>
            <th>DISCOUNT</th>
        </tr>
    </thead>
</table>

<script>
    $.getJSON('pages/lj_menu/monitoring/process/price_history_data.php?custid=<?php echo $cust_id; ?>', function(response) {
        var custid = '<?php echo $cust_id; ?>';
        // Initialize DataTables
        $('#invoice-disc-list').DataTable({
            iDisplayLength : 15,
            processing: true,
            data: response,
            columns: [
              {data: "INVOICE_NO"},
              {data: "INVOICE_DATE"},
              {data: "INVOICE_SALESMAN"},
              {data: "INVOICE_AMOUNT"},
              {data: "INVOICE_DISCOUNT"}
          ]
        });

        // Initialize AJAX onClick Data Send
        window.someGlobalOrWhatever = response.balance;
    });
</script>