1

我有一个 php 代码,当用户删除记录时会发出警报,但它会在发出警报时刷新页面,然后背景页面变为空白,这似乎很烦人。不刷新页面怎么办?下面是我名为 deleteuser.php 的示例代码。

<?
include 'mysqlcon.php'; // my connection credentials to php

$postudel = $_POST['enterudel'];

if($postudel!="")
    {
    $sqldel = "DELETE FROM User WHERE UserName = '$postudel'";
    $getdel = mysql_query($sqldel,$con) or die(mysql_error());
    echo "<script language='javascript'>";
    echo "alert('User $postudel successfully deleted.');";
    echo "</script>";
    }
?>

<html>
<head>
<title>Delete User</title>

<SCRIPT language=JavaScript>
function deluser()
{
var r=confirm("Are you sure you want to do this?");
if (r==true)
    {
    var objsel = document.getElementById("duser");
    var udel = objsel.options[objsel.selectedIndex].id;
    document.formdel.enterudel.value = udel;
    document.formdel.submit();
    }
    else
    return false;
}
</script>
</head>

<body>
<table width="50%" align="left">
<tr><td width="50%" colspan="2">
<big>Delete User:</big>
</td></tr>

<tr><td width="25%">
Username:&nbsp;
<SELECT id="duser">
<option>Choose One</option>
<?
$sqldu=mysql_query("SELECT UserName from User");

while($row=mysql_fetch_array($sqldu))
{
    echo "<OPTION ID=".$row['UserName'];
    echo ">".$row['UserName']."</OPTION>";
}
?>
</SELECT>
</td>

<td width="25%">
<input type="button" value="Delete" id="delete" onClick="deluser()">
</td></tr>
</table>

<form name="formdel" action="../deleteuser.php" method="post">
<input type="hidden" name="enterudel" value="<?=$postudel?>">
</form>

</body>
</html>
4

1 回答 1

2

分裂

if($postudel!="")
    {
    $sqldel = "DELETE FROM User WHERE UserName = '$postudel'";
    $getdel = mysql_query($sqldel,$con) or die(mysql_error());
    echo "<script language='javascript'>";
    echo "alert('User $postudel successfully deleted.');";
    echo "</script>";
    }

分为两部分:

if($postudel!="")
    {
    $sqldel = "DELETE FROM User WHERE UserName = '$postudel'";
    $getdel = mysql_query($sqldel,$con) or die(mysql_error());
    }

现在在哪里,并且

<?
if($postudel!="")
    {
    echo "<script language='javascript'>";
    echo "alert('User $postudel successfully deleted.');";
    echo "</script>";
    }
?>

之前</body>。然后你将删除并绘制警报之前的页面,避免空白页面

于 2012-10-15T02:28:13.890 回答