douxian9706 2014-09-17 21:36
浏览 30
已采纳

单击时通过JQuery传递给PHP脚本的表值

I have a link in a datatable that opens a bootstrap modal. In the modal I have a morris.js graph. I'm having troubles figuring out how to filter the dataset according to what link the user clicked on.

How do I read in the value of the where they click (e.g. Cluster1 or Cluster2 in this example). Pass that variable to the data pull script (e.g. cluster_pulse.php) and then into the SQL query within that script?

Thanks!!

Snip of the table:

<tr class="odd">
<td class="sorting_1">
<a href="#" data-toggle="modal" data-target="#clusterpulse">Cluster1</a></td>...
<a href="#" data-toggle="modal" data-target="#clusterpulse">Cluster2</a></td>...

The modal:

<div class="modal fade" id="clusterpulse" tabindex="-1" role="dialog"
                aria-labelledby="clusterpulse" aria-hidden="true">
   <div class="modal-dialog modal-lg">
      <div class="modal-content">
         <div class="modal-header">
         <button type="button" class="close" data-dismiss="modal"
         aria-hidden="true">&times;</button>
         <h4 class="modal-title" id="myModalLabel">Cluster Pulse</h4>
         </div>
         <div class="modal-body">  
         <div class="panel panel-primary">
         <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-long-arrow-right"></i>Cluster Pulse</h3>
         </div>
         <div class="panel-body">
         <div id="morris-chart-cluster-pulse"></div>
         </div>
        </div>
        </div>
         </div>
       </div>
     </div>

The js:

<script> 
        $('#clusterpulse').on('shown.bs.modal', function () {
            $(function () {
            $( "#morris-chart-cluster-pulse" ).empty();

            // Create a Bar Chart with Morris
            var chart = Morris.Line({
                element: 'morris-chart-cluster-pulse',

                d: [
                    { d: '0', VSI: 0 }
                  ],
                  xkey: 'd',
                  ykeys: ['test'],
                  labels: ['test'],
                gridIntegers: true,
                  ymax: '1',
                  ymin: '0',
                  xLabelFormat: function(d) { return (d.getMonth()+1)+'/'+d.getDate();},
                  xLabels: "week"

            });

            // Fire off an AJAX request to load the data
            $.ajax({
                type: "GET",
                dataType: 'json',
                url: "../scripts/cluster_pulse.php", // This is the URL to the API

            })
                .done(function (data) {
                    // When the response to the AJAX request comes back render the chart with new data
                    chart.setData(data);
                })
                .fail(function () {
                    // If there is no communication between the server, show an error
                    alert("error occured");
                });
        });
        });

Data pull script:

<?php 
include("connect.php");

if( $conn === false ) {
echo "Could not connect.
";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. */
$sql = "SELECT d,test FROM <database> WHERE CLUSTER_NAME = '%$value%'
";
$stmt = sqlsrv_query( $conn, $sql);

do {
 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
 $json[] = $row;

 }
} while ( sqlsrv_next_result($stmt) );
foreach($json as &$each) {
// reassign "d" key to just the date (formatted), discard the rest
$each['d'] = date('Y-m-d', strtotime($each['d']));
}
echo json_encode($json);
?>
  • 写回答

2条回答 默认 最新

  • dsg435665475 2014-09-22 17:18
    关注

    What I ended up doing is added an ID to the table object with the cluster name:

    ...
    <td class="sorting_1">
    <a href="#" data-toggle="modal" data-target="#clusterpulse" id="cluster1">cluster1</a></td>
    ...
    

    Then in javascript I grabbed the ID of the clicked object and assigned to a global variable:

    $("table").delegate("a", "click", function() {
    id=$(this).attr('id');
    }); 
    

    Then in the get I passed the ID to the API:

    $.ajax({
    type: "GET",
    dataType: 'json',
    url: "../scripts/cluster_pulse.php?val="+id, // This is the URL to the API
    

    Then in the php I grabbed the variable and passed it into my query:

    $rec = $_REQUEST['val'];
    ...
    $sql = "SELECT d,test FROM <database> WHERE CLUSTER_NAME = ('".$rec."')";
    

    It seems to be working perfectly. Thanks for all the help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系