0
該代碼成功或錯誤後,應現場更改圖像:使用ajax.js file
// JavaScript Document
var xmlhttp=false;
function claimbonus(user, type, id){
xmlhttp = new XMLHttpRequest();
xmlhttp.abort();
xmlhttp.open("GET", "/functions/ajax.php?func=claim_bonus&user="+user+"&type="+type, true);
xmlhttp.onreadystatechange=function() {
if(xmlhttp.status == 200) {
document.getElementById(id).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
和代碼
if(isset($func) and $func == 'claim_bonus'){
global $ado;
$user = escape($_GET['user']);
$type = escape($_GET['type']);
$now = date("Y-m-d H:i:s");
$points = rand(1,20);
$query = $ado->exec("INSERT INTO `claimed_bonuses` SET `user` = '$user', `date` = '$now', `type` = '$type'");
$query1 = $ado->exec("INSERT INTO `history` SET `user` = '$user', `date` = '$now', `type` = 'bonus', `amount` = '$points', `title` = 'Bonus Claimed', `description` = '$user claimed bonus $points points'");
$query2 = $ado->exec("update `balances` SET `actual` = actual+$points, `total` = total+$points");
if ($query && $query1 && $query2) {
echo "<img src=\"/img/bonus/add_used.png\" width=\"30%\" height=\"30%\" alt=\"Bonus claimed\" />";
} else {
echo "<img src=\"/img/bonus/error.png\" width=\"30%\" height=\"30%\" alt=\"Error\" />";
}
}
林調用Ajax是裝上頁。
腳本正在返回圖像,但它並未取代舊圖像。
圖片代碼:
<img src="img/bonus/add.png" width="30%" height="30%" alt="Claim bonus" id="add_img" onclick="claimbonus(<?php echo $_SESSION['userid']; ?>, '<?php echo $type; ?>', 'add_img'); return false"/>
我希望有人能幫助我
請將您的代碼添加到實際問題,而不是鏈接到它。你還應該儘可能地減少代碼到一個[SSCCE](http://sscce.org),儘可能地嘗試和調試你的代碼,並告訴我們你的代碼的哪些部分可以(不)工作。 – Matt
歡迎來到StackOverflow。請查看[常見問題](http://stackoverflow.com/faq)以獲取有關創建可能會收到良好答案的好問題的信息。請考慮更新您的問題,以包含相關的代碼片段以及您期望發生的事情,現在正在發生的事情以及您所嘗試的內容的完整描述。 –
請看看JQuery libaray,它有一個很好且簡單的方法來處理Ajax請求 – ewein