案例:我創建了sql數據按鈕(while循環),對於每一行結果(machine_id,tag_name,real_value),我創建了一個按鈕。此按鈕有一個onclick方法,它調用JavaScript方法「drawChart()」,並用靜態查詢彈出圖表。我試圖讓這個查詢動態的按鈕被點擊的值。將php變量(點擊)傳遞給另一個php腳本
創建按鈕的代碼片段。 drawChart的
while ($row = mysqli_fetch_array($result)) {
if ($row["int_value"] == 0 && $row["real_value"] == 0.0 && $row["bool_value"] != "") {
$data[] = $row;
$output .= '
<tr>
<td width="30%"><button name="view" value="view" id="id" class="view_btn_data" onclick="drawChart(\'' . $row["tag_name"] . '\')">' . $row["tag_name"] . '</button></td>
<td width="30%">' . $row["bool_value"] . '</td>
<td width="40%">' . $row["datetime"] . '</td>
</tr>
';
}
Javascript代碼():
function drawChart()
{
google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart(tagname) {
var jsonData = $.ajax({
url: "testChart.php",
dataType: "json",
data: {tagname: tagname},
async: false
}).responseText;
var data = new google.visualization.DataTable(jsonData);
var options = {
chart: {
title: 'Values',
},
width: 900,
height: 500
};
//var chart = new google.charts.Line(document.getElementById('chart_div'));
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
//chart.draw(data, google.charts.Line.convertOptions(options));
chart.draw(data, options);
$('#chartModal').modal("show");
}
}
圖得到他testChart.php的數據,下面的查詢:
$tagname = $_REQUEST['tagname'];
$Chart = "SELECT *
FROM (
SELECT *,
@rn := if(@tag_name = tag_name,
@rn + 1,
if(@tag_name := tag_name, 1, 1)
) as tag_count
FROM waardes
CROSS JOIN (SELECT @rn := 0, @tag_name := '') as vars
ORDER BY tag_name
) as T
WHERE tag_count < 51 AND machine_id LIKE 3 AND tag_name LIKE '. $tagname .'
ORDER BY datetime DESC";
它的工作原理,但一個靜態圖,點是,如果單擊按鈕,我想將該$行的參數傳遞給查詢「WHERE tag_count < 51 AND machine_id LIKE 變量$ row [「machine_id」] AND tag_name LIKE 變量$ row [「tag_name」]「。
*對不起,如果它有點混亂,不真正與php,html和javascript家庭。
傳遞的網址你的機器ID 「testChart.php?ID =」。$行[ 'machine_id']和訪問使用$你的PHP頁面_GET ['machine_id'] – pravindot17
我已經編輯上面的代碼..請再看一遍。 – Niels