如何停止PHP脚本并询问用户是否

时间:2016-12-19 11:12:36

标签: php

由于PHP脚本是无状态的,是否有任何技术可以停止脚本并询问用户关键问题? 当我想使用delete.php

从DB中删除记录时,我正在处理询问用户

我的代码示例

 $registerquery = mysql_query("UPDATE users SET service = '".$service."' WHERE Username = '".$username."'");

3 个答案:

答案 0 :(得分: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&action=edit

<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&action=delete

$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;
}

经过战斗测试的CRUD

您可能需要查看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

答案 1 :(得分:1)

您可以使用JavaScript。将代码内联,放入函数或使用jQuery。

1.Inline:

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

2.在一个功能中:

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

脚本代码

<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 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>

答案 2 :(得分: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>