2016-12-19 39 views
0

由於PHP腳本是無狀態的,有沒有什麼技術可以停止腳本並詢問用戶關鍵問題? 我處理要求的用戶,當我想從數據庫中刪除記錄使用delete.php如何停止PHP腳本並詢問用戶是否

我的代碼示例如下

<?php 
    if(isset($_GET['id']) && ctype_digit($_GET['id'])) { 
     $id=$_GET['id']; 
    } 
    else header('Location: select.php'); 

    include 'dbconn.php'; 
    $sql = "DELETE FROM users WHERE id='$id'"; 
    echo "Are you sure?"; 
    $del=false; 
    echo "<a href='$del=true'>yes</a> <a href='$del=false'>no</a>"; 
    if($del) $result = mysqli_query($conn,$sql); 
    mysqli_close($conn); 
    header('Location: select.php'); 
+0

這通常是通過JavaScript完成。你創建一個請求確認的彈出窗口,然後向服務器發送一個AJAX請求;讀取服務器響應並通知用戶有關成功或失敗的操作。 –

+0

按下按鈕客戶端,彈出詢問是否確定。如果確認,處理。如果沒有,不要。簡單 –

+0

不使用HTTP,至少不是很容易。你最好打賭它有一個預先刪除屏幕/模式,其中說「你確定嗎?」是/否,然後導致上述屏幕。 – Farkie

回答

0

你嘗試這樣.....

<?php 
if(isset($_GET['id']) && ctype_digit($_GET['id'])) { 
     $id=$_GET['id']; 
    } 
else header('Location: select.php'); 

include 'dbconn.php'; 
$sql = "DELETE FROM users WHERE id='$id'"; 

print "<script> var delete= window.confirm('are you sure to delete?')</script>"; 
?> 
<script> 
    if(delete==true){ 
     <?php 
//your php code for if block 
//for your problem code can be 
    $result = mysqli_query($conn,$sql); 
    mysqli_close($conn); 
    header('Location: select.php'); 

     ?> 
    } 
    else{ 
     <?php 
//your php code for else block 
    ?> 
    } 
</script> 
+0

這真的很清楚/漂亮的解決方案。這對我有用。謝謝! – zoladp

+0

這是可怕的損壞,並會刪除用戶每次 – naomik

+1

它將刪除用戶,如果你點擊yes確認..其他部分將執行。 –

1

你可以使用JavaScript。將代碼嵌入到函數中或使用jQuery。

1.Inline:

<a href="deletelink" onclick="return confirm('Are you sure?')">Delete</a> 

2.In的函數:

<a href="deletelink" onclick="return checkDelete()">Delete</a> 

Script代碼

<script language="JavaScript" type="text/javascript"> 
function checkDelete(){ 
    if(confirm('Are you sure?')) 
    { 
     alert("You clicked YES"); 
    } 
    else 
    { 
     alert("You clicked NO"); 
    } 
} 
</script> 

3.具有的jQuery:

<a href="deletelink" class="delete">Delete</a> 

Script代碼:

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
<script language="JavaScript" type="text/javascript"> 
$(document).ready(function(){ 
    $("a.delete").click(function(e){ 
     if(confirm('Are you sure?')) 
     { 
      alert("You clicked YES"); 
      return true; 
     } 
     else 
     { 
       alert("You clicked NO"); 
       return false; 
     }    

    }); 
}); 
</script> 
+0

我只是JS和JQuery中的新手,但這是未來可以使用的東西。 Apreciate! – zoladp

3

的建議使用JavaScript都很好,但知道這不是唯一的選擇。

下面是一個工作流程,可以爲您工作,只使用PHP和每個操作的不同頁面請求。

/users.php?id=5

<h1>Viewing <?php $_GET['id'] ?></h1> 
... 
<a href="/users.php?id=5&action=edit">Edit this user</a> 

/users.php?id=5 &行動=編輯

<h1>Editing <?php $_GET['id'] ?></h1> 
... 
<a href="/users.php?id=5">Cancel edits</a> 
<a href="/users.php?id=5&action=save">Save changes</a> 
<a href="/users.php?id=5&action=deleteConfirmation">Delete this user</a> 

/users.php?id= 5 & action = deleteConfirmation

<h1>Are you sure you want to delete <?php echo $_GET['id'] ?></h1> 
<a href="/users.php?id=5">Cancel</a> 
<a href="/users.php?id=5&action=delete">Confirm</a> 

/users.php?id=5 &行動=刪除

$sql = "DELETE FROM users ..." 
mysqli_query($sql) ... 
header('Location: /users.php?action=deleteSuccessful'); 

users.php可能看起來像這樣(僞)

switch ($_GET['action']) { 
    case 'edit': 
    <h1>Editing using...</h1> 
    <form> ... 
    break; 
    case 'save': 
    mysqli_query('UPDATE USERS SET ...'); 
    header('Location: ...'); 
    break; 
    case 'deleteConfirmation': 
    <h1>Are you sure you want to delete user 5</h1> 
    <form> ...; 
    break; 
    case 'delete': 
    mysqli_query('DELETE FROM USERS ...'); 
    header('Location: ...'); 
    break; 
    default: 
    <h1>Viewing User 5</h1> 
    ... 
    break; 
} 

經過對照測試的CR UDs

你可能想看看RESTful APIs。有一些約定用於爲URL設置資源,然後使用不同的HTTP動詞與資源進行交互。

這是一個基本速成班的理論用戶資源

http  url    description 
GET  /users   display all users 
POST  /users   create a new user 
GET  /users/1   display user with id: 1 
GET  /users/1/edit display the edit user page 
PUT  /users/1   replace all the fields for user id: 1 
PATCH /users/1   update 1 or more fields for user id: 1 
DELETE /users/1   remove user with id: 1 
+0

哇,這是好主意,而不是使用不同的網頁,謝謝。 – zoladp

+0

@zoladp沒問題。它只是您設計事物的衆多方式之一。看看我在答案的第二部分添加的RESTful筆記。儘管一些新技術正在挑戰REST的侷限性,但REST一直是資源API設計的黃金標準。我鼓勵你看看它,因爲內部仍然有很多有用的智慧。 – naomik

+0

好吧,會看看REST。 – zoladp