简体   繁体   中英

Enable/Disable button that changes when clicked and executes SQL

So basically I am using PHP/AJAX/JavaScript/SQL, and I'm new to it all, to display a list of all my users with a button beside each on that says 'Enable' if there is a 1 in my 'Enabled' column in the database or 'Disable' if there is a 0. The button beside each user displays properly when I first load the page, and if the user has an 'Enable' button, it does enable the user when I click on it, the text changes to 'Disable' but it doesn't disable the user when clicked.

If the user has a 'Disable' button when the page is first loaded, clicking it will change it to blank and the user is not disabled.

I can't figure out where I'm going wrong. I'll include my code so far below.

Code for enable/disable button in adminPage.php

if($enabled=='1'){
            $allUsers.='<button id="adminButton_'.$username.'" onclick="adminHandler(\'disable\',\''.$username.'\',\'adminButton_'.$username.'\')">disable</button>';

            }
            else if($enabled=='0')
            {
              $allUsers.='<button id="adminButton_'.$username.'" onclick="adminHandler(\'enable\',\''.$username.'\',\'adminButton_'.$username.'\')">enable</button>';

            } 

adminHandler function in adminPage

 <script type="text/javascript">

function adminHandler(action,username,elem){
    var conf = confirm("Press OK to '"+action+"' this user.");
    if(conf != true){
        return false;
    }
    _(elem).innerHTML = "processing ...";
    var ajax = ajaxObj("POST", "Includes/adminProcessing.php");

    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            if(ajax.responseText == "enable_ok"){
                _(elem).innerHTML = "Disable";
            } else if(ajax.responseText == "disable_ok"){
                _(elem).innerHTML = "Enable";
            } else {
                _(elem).innerHTML = ajax.responseText;
            }
        }
    }
    ajax.send("action="+action+"&username="+username);
}

The adminProcessing.php page

if (isset($_POST['action']) && isset($_POST['username']))
    {
        $username = $_POST['username'];


        if($_POST['action'] == "enable")
        {

            $sql = "UPDATE users SET enabled='1' WHERE username='$username' LIMIT 1";


            $query = mysqli_query($conn, $sql);

            mysqli_close($conn);
            echo "enable_ok";
            exit();
        }

        else if($_POST['action'] == "disabled"){
              $sql = "UPDATE users SET enabled='0' WHERE username='$username' LIMIT 1";


            $query = mysqli_query($conn, $sql);

            mysqli_close($conn);
            echo "disable_ok";
            exit();
        }

AJAX File

function ajaxObj(meth, url) {
    var x = new XMLHttpRequest();
    x.open(meth, url, true);
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    return x;
}
function ajaxReturn(x) {
    if (x.readyState == 4 && x.status == 200) {
        return true;
    }
}

您的“ adminHandler”正在传递“ disable”,但您正在检查“ disabled”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM