警报用户无需在PHP中刷新页面

时间:2012-10-15 02:13:49

标签: php javascript ajax

我有一个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>

1 个答案:

答案 0 :(得分: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>之前

。然后,您将在警报之前删除并绘制页面,避免空白页