2012-10-15 54 views
1

我有一個php代碼,當用戶刪除一條記錄時會發出警報,但它會在提醒時刷新頁面,然後背景頁面變爲空白,這看起來很煩人。我怎樣才能不刷新頁面?以下是我的示例代碼,名稱爲deleteuser.php。提醒用戶不刷新頁面中的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

AJAX =沒有刷新的東西。 –

+0

嘗試在http://api.jquery.com/jQuery.ajax/上使用jquery ajax/ – Habibillah

回答

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>之前。然後,您將在警報之前刪除並繪製頁面,從而避免空白頁面

+0

非常感謝你,它的工作原理。 – Ryan