我正在尝试将 json_encode 内的数据附加到在 PHP WHILE 循环中创建的 DIV。更清楚here is my fiddle for demonstration 。
我有一个具有唯一 ID 的 DIV: table (您看到的小盒子)。 当我的“aht”按钮被点击时,它会运行 show_aht.php。
我希望 show_aht.php 中的数据“时间”替换每个适当的“桌面”DIV 内的数字,因此显示从脚本中检索到的“时间”值。
问题:它没有显示,我在如何使用我的唯一 ID 附加/替换时遇到问题
提前致谢!
map.php WHILE 循环执行查询以创建 Desk 和 Station DIV:
//desks
while($row = mysqli_fetch_assoc($desk_result)){
//naming values from coordinates table
$id = $row['coordinate_id'];
$x_pos = $row['x_coord'];
$y_pos = $row['y_coord'];
$sec_name = $row['section_name'];
$sta_num = $row['station_number'];
$position = $row['ver_hor'];
//draw a DIV box at its X,Y coordinate
//if the desks belong in the section names below then assign the class name to make desks horizontal
$class = "desk_box_ver"; //by default make desks vertical, by assigning class name
if($position == "horizontal"){
$class = "desk_box_hor";//assign the class name to make desks horizontal
}
这是我希望数据“时间”通过使用 id = test_$id 替换 $sta_number 值
//output all desks being read from the query
echo '<div class="' . $class . ' id="test_'.$id.'" data-rel="' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos.'px;">' . $sta_num . '</div>' . "\n";
}//end while
map.php AJAX 如何在 DIV 下方的 FOR 循环中附加 id = test_$id?
<div id="aht"><!--aht button-->
<button id="aht_button">AHT</button>
</div><!--aht button-->
<script type="text/javascript">
$(document).ready(function() {
$('#aht').click(function(){
$.ajax({
type:"GET",
url : "show_aht.php",
data:{} , // do I need to pass data if i'm GET ting?
dataType: 'json',
success : function(data){
alert(data);
//NOT WORKING PLEASE HELP
for(i = 0; i < data.length; i++){
$("#test_"+id).html(data[i])
}
}//end success
});//end ajax
});//end click
});//end rdy
</script>
show_aht.php - json_encode 部分
/**comparing usernames from both arrays*/
$res = array();
foreach($memo_data as $m){
foreach($user_data as $u){
//if theres a matching username
//display user and time
if( ($m['memo_code'] == $u) && ($m['avg_handle_time'] != null) ){
$res[] = substr($m['avg_handle_time'],0,-3);//removing decimals
}
//else if matching user but no time, do not display time
elseif( ($m['memo_code'] == $u) && ($m['avg_handle_time'] == null) ){
$res[] = "NA";
}
}
}
$final_res = json_encode($res);
echo $final_res;
最佳答案
我会按照@jeroen 的建议去做。 console.log(data);
成功后。您还可以尝试 complete: function(data) { ...
并记录数据,这样总会有结果。
此外,您使用 POST
是否有特殊原因?在我看来,您正在GET
获取数据。
关于javascript - PHP 和 AJAX : How to append json_encode data to multiple unique IDs <DIV>'s?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26850144/