javascript - PHP 更改按钮文本并相应更新数据库

标签 javascript php mysql

我的函数如何工作:

有 2 个按钮“接受”和“拒绝”。当用户单击“接受”时,文本将更改为“已接受”,并且两个按钮都将被禁用。文字的颜色也会改变。在此过程之后,我需要相应地更新数据库表列。

我目前的情况:

我正在显示多个具有此功能的条目。 目前,我的代码仅在我有一个条目时才起作用,并且当我刷新页面后按下它时,它不会保持应有的状态。当有多个条目时,我在第二个条目上单击的按钮会以某种方式检测第一个条目并更改第一个条目中的按钮。 我不知道如何相应地更新数据库。

提前非常感谢您。

我的脚本:

<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:
使用 JQuery AJAX 使其变得更加容易。
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 } ?>

关于javascript - PHP 更改按钮文本并相应更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590849/

相关文章:

javascript - 未处理的 Promise 拒绝 : Cannot read property 'push' of undefined Value

javascript - lambda 长深

php - Symfony 实体类似乎不是托管的 Doctrine 实体

php - 根据每个分类的多重评级获取帖子

php - MYSQL delete where value = multiple POST 值

mysql - Heroku 和 NodeJs - MySql 连接丢失 : The server closed the connection

javascript - 使用 Selenium 在特殊元素上执行 JavaScript querySelectorAll

javascript - chrome 中的功能无法正常工作

php - Symfony2 PHPUnit 时钟模拟不起作用

php - 相同参数中的 JSON_PRETTY_PRINT 和 JSON_UNESCAPED_SLASHES