喜歡我在數據庫如何將此值傳遞給JavaScript並在HTML頁面上一一顯示?
100個值我想這個值通過PHP使用AJAX傳遞給JavaScript。
PHP CODE:
for($i=0;$i<=9;$i++)
{
$random = (10 * $i) + rand(1,10);
$result = mysql_query("SELECT * FROM check_val WHERE id = '$random'") or exit(mysql_error());
if(mysql_num_rows($result) == 1){
$row = mysql_fetch_assoc($result);
$value = $row['val'];
$alpha = $row['alp'];
}
$arr = array("val"=>$value,
"alp"=>$alpha
);
echo json_encode($arr)."<br>";
}
OUTPUT:
{"val":"1","alp":"A1"}<br>
{"val":"15","alp":"A15"}<br>
{"val":"23","alp":"A23"}<br>
{"val":"37","alp":"A37"}<br>
{"val":"42","alp":"A42"}<br>
{"val":"51","alp":"A51"}<br>
{"val":"67","alp":"A67"}<br>
{"val":"71","alp":"A71"}<br>
{"val":"84","alp":"A84"}<br>
{"val":"100","alp":"A100"}<br>
AJAX代碼:
$.ajax({
url: "index.php",
type: "post",
dataType: "json",
success: function(response) {
$("#val").html(response.val);
$("#alp").html(response.alp);
}
});
這AJAX代碼用於當O本安輸出是這樣的:
{"val":"1","alp":"A1"}
即只是一個單一的價值
我如何可以讀取所有的值,並點擊在HTML頁面的按鈕時1顯示出來1。
HTML代碼:
<p id="val"></p><br>
<p id="alp"></p><br>
<button>Click Me</button>
因此,第一次點擊按鈕「{」val「:」1「,」alp「:」A1「}'出現。第二次點擊時,出現「{」val「:」15「,」alp「:」A15「}」等等? – Jamy
是的@JamyGolden請幫忙...從這幾天卡住 – Ankit