2012-03-26 67 views
0

表單會刪除存儲在MySql數據庫中的所有記錄,例如example.php?id=7等頁面上與過濾後的ID相關聯的記錄,並在成功刪除記錄時重定向到相同的頁面。如何在成功刪除後顯示錶單操作狀態消息?

代碼的細節如下:

if ((isset($_POST['id'])) && ($_POST['id'] != "")) { 
    $deleteSQL = sprintf("DELETE FROM users WHERE id=%s", 
         GetSQLValueString($_POST['id'], "int")); 


    mysql_select_db($database_example, $example); 
    $Result1 = mysql_query($deleteSQL, $example) or die(mysql_error()); 


    $deleteGoTo = "example.php"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; 
    $deleteGoTo .= $_SERVER['QUERY_STRING']; 
    } 
    header(sprintf("Location: %s", $deleteGoTo)); 
} 

$colname_deleterecord = "5"; 
if (isset($_GET['id'])) { 
    $colname_deleterecord = $_GET['id']; 
} 
mysql_select_db($database_example, $example); 
$query_deleterecord = sprintf("SELECT id, user_name, email, address FROM users WHERE id = %s", GetSQLValueString($colname_deletetrecord, "int")); 
$deleterecord = mysql_query($query_deleterecord, $example) or die(mysql_error()); 
$row_deleterecord = mysql_fetch_assoc($deleteecord); 
$totalRows_deleterecord = mysql_num_rows($deleterecord); 

<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <label> 
     <input type="submit" name="Delete Record" id="Delete Record" value="Delete Record" /> 
    </label> 
    <input name="id" type="hidden" id="id" value="<?php echo $row_deleterecord['id']; ?>" /> 
    </form> 

如何顯示錶單的動作狀態消息像(「記錄已刪除」)與記錄後純PHP語法在同一頁上成功刪除?

+0

what do you mean with「on the same page」?在我看來,你在刪除後重定向 – 2012-03-26 09:05:11

+0

成功的表單操作'header(sprintf(「Location:%s」)後頁面被重定向到同一頁面,$ deleteGoTo));' 'action =「<?php echo $ _SERVER ['PHP_SELF'];?>'。 – polarexpress 2012-03-26 10:59:57

回答

0

您可以存儲動作消息到會話..你和展架這樣的,如果你使用任何MVC架構: -

$this->Session->setFlash('Record Deleted.'); 

試試這個..

0

我終於解決了這個問題。

訣竅是將一個查詢字符串(鍵/值對)分配給頭位置變量或直接用它的相關值替換頭位置變量。

然後切換與函數和表單變量關聯的查詢字符串(鍵/值對),並用它包裝一個div,在刪除成功後應在頁面上顯示錶單動作狀態消息。