嘿,我有一些问题。
我的网站分为两栏。左边是侧边栏,其中包含从数据库动态生成的用户列表,右侧应该是基于user_id的javascript框架(ajax)生成的唯一图表。从列表中选择一些用户后,应显示此图表。这个javascript / ajax使用的php文件live-data.php需要GET参数。现在是:
url:“php / live-data.php”
和
$。get(“php / live-data.php?Consultar = 1”,功能(UltimosDatos)
但它应该是
url:“php / live-data.php?user_id = 2”
和
$。get(“php / live-data.php?user_id = 2& Consultar = 1”,function(UltimosDatos)
从动态生成的列表中单击某个用户名后,其中2是user_id。 php脚本live-data.php已准备好用于GET变量,并为图表框架返回正确的json(此javascript如下所示)。我不知道如何将div id传递给这个ajax代码。
HTML + PHP:
<div id="left" class="pre-scrollable col-lg-3">
<div class="list-group">
<?php include("php/dbSettings.php");
$result = $conn->query("SELECT * FROM user ORDER BY user_id");
if (!$result) {
die(mysqli_error($conn));
}
while ($user = mysqli_fetch_array($result)){
echo '<a href="#'.$user['user_id'].'" data-toggle="tab" class="list-group-item">' . $user['firstName'] . " " .$user['lastName'] . '</a>';
}
?>
</div>
</div>
<div id="right" class="col-lg-9">
<div class="tab-content">
<?php include( "php/dbSettings.php");
$result=$ conn->query("SELECT * FROM users ORDER BY user_id");
if (!$result) {
die(mysqli_error($conn));
}
while ($user = mysqli_fetch_array($result)){
echo '<div class="tab-pane" id="'.$user['user_id'].'">
<div id="chart" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
</div>';
} ?>
</div>
</div>
的Javascript / AJAX:
<script>
$(function() {
$(document).ready(function() {
var ultimox;
var ultimoy;
$.ajax({
url: "php/live-data.php", //i want this line to be "php/live-data.php?user_id=2" and 2 is variable got from user list onlick
type: 'get',
success: function(DatosRecuperados) {
$.each(DatosRecuperados, function(i, o) {
//some deleted code - unimportant
});
//some deleted code - unimportant
$('#chart').highcharts({
//draws chart
});
}
});
});
setInterval(function() {
$.get("php/live-data.php?Consultar=1", function(UltimosDatos) { //i want this line to be "php/live-data.php?php/live-data.php?Consultar=1&user_id=2" and 2 is variable got from user list onlick
//updates chart
}
});
}, 1000);
//some deleted code - unimportant
});
</script>
我希望有人可以帮助我。
谢谢,保罗
答案 0 :(得分:0)
根据此<a href="#'.$user['user_id'].'"
,在单击锚点时,哈希将被设置为用户ID,因此您可以读取哈希值并将其作为请求中的数据传递。要么:
$.ajax({
url: "php/live-data.php?user_id=" + window.location.hash.substr(1),
type: 'get',
success: function(DatosRecuperados) {
// ...
}
});
&#13;
或者这个:
var dataObj = {};
dataObj['user_id'] = window.location.hash.substr(1); //create a data object to pass on query string, set user id value
$.ajax({
url: "php/live-data.php",
type: 'get',
data: dataObj, //pass object with request
success: function(DatosRecuperados) {
// ...
}
});
&#13;