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条)

报告相同问题?

悬赏问题

  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。
  • ¥20 在easyX库下编写C语言扑克游戏跑的快,能实现简单的人机对战
  • ¥15 svpwm波形异常求解答
  • ¥15 STM32——硬件IIC从机通信代码实现
  • ¥15 微生物组数据分析--微生物代谢物
  • ¥30 求一跃动小子保卫主公Java算法实现
  • ¥15 地图软件开发技术答疑(api, 地点获取,外观样式)
  • ¥20 物理远程控制麦克风使用问题