我的函数如何工作:
有两个按钮"接受"answers"拒绝"。当用户单击"接受"时,文本将变为"已接受",并且两个按钮都将被禁用。文本的颜色也会改变。在这个过程之后,我需要相应地更新数据库表列。
我目前的状况:
我正在显示多个具有此功能的条目。目前,我的代码只有在我有一个条目时才起作用,刷新页面后按下它时,它不会保持应有的状态。当有多个条目时,我在第二个条目上单击的按钮会以某种方式检测到第一个条目,并更改第一个条目中的按钮 我不知道如何相应地更新数据库
提前非常感谢。
我的脚本:
<script>
function accept() {
document.getElementById("accept").disabled = true;
document.getElementById("decline").disabled = true;
document.getElementById("accept").innerHTML = 'Accepted';
document.getElementById("accept").style.color = "green";
}
function decline() {
document.getElementById("decline").disabled = true;
document.getElementById("accept").disabled = true;
document.getElementById("decline").innerHTML = 'Declined';
document.getElementById("decline").style.color = "red";
}
</script>
接受按钮:
<button id="accept" onclick="accept()">Accept</button>
拒绝按钮:
<button id="decline" onclick="decline()">Decline</button>
您需要学习如何使用AJAX:
使用JQueryAJAX使它变得更加容易
JS文件:
$.ajax({
url: "/updateDatabase.php";
type: "POST";
data: {update: value},
beforeSend: function (){
//stuff you like to do before sending.
}
success: function (data){
//do something with return data
}
});
PHP文件:updateDatabase.PHP
$var = $_POST["update"]; //make sure this is the same name for the data{} json string
//update database.
echo "Put return value here for JS success data var."
记住按钮状态:
<?php if(databaseValue == Accepted) { ?>
<button>Format Button Disabled for accepted</button>
<?php } else { ?>
<button>Format button for enabled</button>
<?php } ?>
如果函数的50%操作相同,为什么要编写两个函数。我建议你只写一个函数,并在其中放入一个条件语句,检查点击了哪个按钮。这是一个高效的编程!
关于你的问题,你能在这里分享一张截图吗?操作时到底显示了什么。