我正在尝试做的事情:
问题:
按钮完全没有变化,只有在刷新页面后才会发生。
我尝试使用echo "<meta http-equiv='refresh' content='0'>";
,但这会使页面刷新两次
我知道还有另一种使用Ajax的方法,但是我对此一无所知。那么如何更改按钮?
<?php
$sql = "SELECT * FROM users WHERE username != 'qq'";
$result = mysqli_query($conn, $sql);
// ver se ha users
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
if ($row['estado'] == 1) {
echo '<tr>
<td>' . $row["userID"] . '</td>
<td>' . $row["username"] . '</td>
<td>' . $row["email"] . '</td>
<td>' . $row["pnome"] . '</td>
<td>' . $row["lnome"] . '</td>
<td><form method="post" >
<input type="hidden" value="' . $row["userID"] . '" name="id" />
<button class="btn btn-danger" type="submit" name="desativar">Change Status To Deactivated</button></form></td>
</tr>';
} else {
echo '<tr>
<td>' . $row["userID"] . '</td>
<td>' . $row["username"] . '</td>
<td>' . $row["email"] . '</td>
<td>' . $row["pnome"] . '</td>
<td>' . $row["lnome"] . '</td>
<td><form method="POST">
<input type="hidden" value="' . $row["userID"] . '" name="id" />
<button class="btn btn-success" type="submit" name="ativar">Change Status Activated</button></form></td>
</tr>';
}
if (isset($_POST['desativar'])){
$id = $_POST['id'];
$sql = "UPDATE users SET estado = 0 WHERE userID=".$id;
mysqli_query($conn, $sql);
}elseif (isset($_POST['ativar'])){
$id = $_POST['id'];
$sql = "UPDATE users SET estado = 1 WHERE userID=".$id;
mysqli_query($conn, $sql);
}
}
} else {
echo '<tr>
<td>NO USERS</td>
<td>NO USERS</td>
<td>NO USERS</td>
<td>NO USERS</td>
<td>NO USERS</td>
<td>NO USERS</td>
</tr>';
}
?>
答案 0 :(得分:0)
将PHP放在一个单独的文件中,然后转到该文件的AJAX调用看起来像这样:
function updatedb(val) {
var userID = document.getElementById('userID').value;
var username = document.getElementById('username').value;
var email = document.getElementById('email').value;
var pnome = document.getElementById('pnome').value;
var lnome = document.getElementById('lnome').value;
jQuery.ajax({
type: "POST",
url: "script_to_update_db.php",
data: 'userID=' + userID + '&username='+username + '&email='+email + '&pnome='+pnome + '&lnome='+lnome,
success: function(data){
jQuery("#updated-btn").html(data);
}
});
}
并通过PHP访问此信息-只需将其定位为这样:
$userID = $POST['userID']
...
...
...
在更新数据库和按钮(颜色和文本)之后,您可以实现类似的功能来加载td标签的值。
希望有帮助! :)