各位大大好:
ajax 將資料用php寫回資料庫,並呈現在網頁上(呈現出來的資料沒問題),但console.log會出現下列問題
若將i改成0會出現,console.log就會正常,但我是想要用將全部的陣列顯示在網頁上,所以將變數改為i。
想問各位大大有無方法,謝謝!
for(i=0;i<=a;i++){
document.getElementById(bdate).innerHTML +='<h id="'+obj.show[0].num+'" onclick="showblock(this)">•'+obj.show[0].timest+' '+obj.show[0].class+'<br></h>'; }
$.ajax({
type: "POST",
url: "writeback.php",
async:false,
data: {
"lab":lab,
"bdate":bdate,
"clr":clr,
"timest":timest,
"timeov":timeov,
"mdata":mdata,
},
success: function(info) {
$("#cca").fadeOut(4000);
var obj=JSON.parse(info);
console.log(obj);
var a=obj.length;
var i=0;
$("#" + bdate).find("h").each(function(){
$(this).remove();
});
if(a<6){
for(i=0;i<=a;i++){
document.getElementById(bdate).innerHTML +='<h id="'+obj.show[i].num+'" onclick="showblock(this)">•'+obj.show[i].timest+' '+obj.show[i].class+'<br></h>'; }
}
}
});
});
}
writeback.php
$sql3="select * from ".$mdata." where bdate= :bdate order by timest asc";
$sql4 =$conn->prepare($sql3);
$ar_val=array('bdate'=>$bdate);
if($sql4->execute($ar_val)){
$show=[];
while($row=$sql4->fetch())
{
$show[] = array('timest'=>$row['timest'],'class'=>$row['clr'],'num'=>$row['識別碼'],'keynum'=>$row['keynum'],'keyturn'=>$row['keyturn']);
}
}
$sql5="select * from ".$mdata." where bdate= :bdate order by timest asc";
$sql6 =$conn->prepare($sql5);
$ar_val3=array('bdate'=>$bdate);
if($sql6->execute($ar_val3))
{
while($row1=$sql6->fetchAll())
{
$numm=count($row1);
}
echo json_encode(array('show'=>$show,'length'=>$numm),JSON_UNESCAPED_UNICODE);
}
$conn=null; //結束對象資源
}
var a=obj.length;
for(i=0;i<=a;i++)
假設你的物件長度是3,你的for迴圈會跑4次,0、1、2、3,但你的物件長度只有3,這樣會不會報錯?