[英]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. 所以基本上我使用的是PHP / AJAX / JavaScript / SQL,这是我的新手,如果我的“已启用”中有1,则显示所有用户的列表,每个用户旁边都有一个按钮,显示“启用”数据库中的“”列;如果为0,则为“禁用”。当我第一次加载页面时,每个用户旁边的按钮都会正确显示;如果用户具有“启用”按钮,则当我单击该页面时它会启用用户,文字将变为“禁用”,但在单击时不会禁用用户。
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 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 adminPage中的adminHandler函数
<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 adminProcessing.php页面
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 AJAX文件
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”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.