1

Since PHP scripts are stateless is there any technique to stop script and ask user crucial question? I'm dealing with ask user when a I want to delete record from DB using delete.php

my code example below

<?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');
4
  • This is usually done via JavaScript. You create a popup asking for confirmation, then send an AJAX request to the server; read the server response and inform the user about successful or unsuccessful operation. Commented Dec 19, 2016 at 11:14
  • Press button client side, pop up asking if sure. If confirm, process. If not, don't. Simple Commented Dec 19, 2016 at 11:14
  • No using HTTP, at least not very easily. Your best bet it to have a pre-delete screen / modal, which says "Are you sure?" Yes / No, then it leads to the above screen. Commented Dec 19, 2016 at 11:14
  • use ajax that will help you to accomplish this. Commented Dec 19, 2016 at 11:14

2 Answers 2

3

The suggestions to use JavaScript are fine, but know that's not the only choice.

Here's a workflow that could work for you that's using only PHP and a different page request for each action.

/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 could look something like this (pseudocode)

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

Battled-Tested CRUDs

You might want to look into RESTful APIs. There's conventions for setting up a resource for a URL and then using different HTTP verbs to interact with the resource.

Here's a basic crash course for a theoretical User resource

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
Sign up to request clarification or add additional context in comments.

3 Comments

wow, thats great idea to use different pages instead, thank you.
@zoladp no problem. it's just one of the many ways you can design things. Take a look at the RESTful notes I added on the second portion of my answer. Though some new technologies are challenging the limits of REST, REST has been the gold standard for resource API design for quite some time now. I encourage you to look at it because there's still a lot of useful wisdom contained within.
Ok, will do look REST.
0

Did you try like this.....

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

5 Comments

This really clear/nifty solution. This will work for me. Thank you!
This is horribly broken and will delete the user every time
it will delete the user if you click yes on confirm..otherwise else part wll be executed.
and if you are is much more familiar with JS and jQuery..I think this is the best way for you..
Nah, you misunderstand how PHP serves a static page. JavaScript cannot interact with PHP without sending an async request or using a websocket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.