我需要一个按钮onClick
的帮助。
<button type="button" id="1" onclick="this.disabled=true; ^FUNCTION TO UPDATE^">
<button type="button" id="2" onclick="this.disabled=true; ^FUNCTION TO UPDATE^">
<button type="button" id="3" onclick="this.disabled=true; ^FUNCTION TO UPDATE^">
使用此代码我禁用了按下的按钮,但我还需要在发送id的数据库中更新信息,如下所示:
UPDATE reservation SET status='approved' WHERE id=^ID OF THE CLICKED BUTTON^
我需要将其加载到同一页面,而不是使用POST
或GET
发送。
答案 0 :(得分:1)
答案 1 :(得分:0)
使用普通的javascript,您可以执行以下操作:
<script type="text/javascript">
<!--
var page = "dbupdate.php"; // hardcode this elsewhere out of sight
function update(target,data){
document.getElementById(target).innerHTML = 'sending...';
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = function() {ajaxDone(target);};
req.open("POST", page, true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send("data=" + data );
// IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = function() {ajaxDone(target);};
req.open("POST", page, true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send("data=" + data );
}
}
}
function ajaxDone(target) {
// only if req is "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200 || req.status == 304) {
results = req.responseText;
document.getElementById(target).innerHTML = results;
} else {
document.getElementById(target).innerHTML="ajax error:\n" +
req.statusText;
}
}
}
// -->
</script>
对于输出,请确保您有一个id等于target的div。