Delete Confirmation Challenge from Sitepoint Manual

Hello forum,

I’m still new to PHP and I’m hoping someone can help me with a challenge that is mentioned on page 207 of Build Your Own Database Driven Web Site Using PHP and MySQL, the 4th Edition. I’m sure it’s quite easy, however I just can’t figure it out.

The challenge is to add a confirmation prompt via PHP. The book states that you “modify the controller to respond to the delete button by simply displaying another template, this one prompting the user to confirm the action. When the user submits the form in this page, it should trigger the code in the controller to actually delete the data.” The book also states that the “second form will also have to submit in a hidden field the ID of the author to be deleted.”

Here is start of the controller content page for deleting the author (index.php):


If (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Delete’)
{
…delete code
}

header(‘Location: .’);
exit();

Here is body of the page that displays the list of authors (authors.php):


<body>
<h1>Manage Authors</h1>
  <p><a href="?add">Add new author</a></p>
  <ul>
    <?php foreach ($authors as $author): ?>
      <li>
        <form action="" method="post">
          <div>
            <?php htmlout($author['name']); ?>
              <input type="hidden" name="id" value="<?php echo $author['id']; ?>"/>
              <input type="submit" name="action" value="Edit"/>
              <input type="submit" name="action" value="Delete"/>
          </div>
        </form>
      </li>
    <?php endforeach; ?>
  </ul>
  <p><a href="..">Return to JMS home</a></p>
</body>

I’m assuming that I need a third page that would be the confirmation page, like a confirm.php to prompt the user? Thank you again for reading my post and any help on how I would modify/create all three pages would be greatly appreciated. --Ben

When you click the delete button you can send the user to a simple form with a prompt and confirm/cancel buttons. The confirm form should submit the author ID, action = delete, another value that indicates the user did in fact confirm the deletion.

Then when the data is passed back to the controller you can check for the action value, then for the confirmation. If it is true then delete the author.


check for action = delete
    check for confirmation
        // delete
    else
        // send to confirmation form

Does that help at all?

This is how I do actually.

          &lt;input type="submit" name="action" value="Delete" onclick="return confirm('Are you sure you want to delete?')"/&gt;

thank you for the replys. Sharks Sri, I really like the super simplicity of your reply–it works beautifully. However, I’m assuming that it is done via JS, I’m still new so correct me if I’m mistaken. I really would like to do this via pure PHP if possible, according to the manual.

Jeremy C, this is what I have so far, however I get an error and still need further help…

Controller page or author (index.php):


if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
$id = mysqli_real_escape_string($link, $_POST['id']);
include 'confirm.php';
exit();
}

If (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Yes’)
{
…delete code
}

header(‘Location: .’);
exit();

Here is body of the page that displays the list of authors (authors.php)–nothing changed here:


<body>
<h1>Manage Authors</h1>
  <p><a href="?add">Add new author</a></p>
  <ul>
    <?php foreach ($authors as $author): ?>
      <li>
        <form action="" method="post">
          <div>
            <?php htmlout($author['name']); ?>
              <input type="hidden" name="id" value="<?php echo $author['id']; ?>"/>
              <input type="submit" name="action" value="Edit"/>
              <input type="submit" name="action" value="Delete"/>
          </div>
        </form>
      </li>
    <?php endforeach; ?>
  </ul>
  <p><a href="..">Return to JMS home</a></p>
</body>

and here is the body of the new confirmation page (confirm.php):


<body>
    <p>
      Are you sure you want to delete?
      <input type="hidden" name="id" value="<?php echo $item['id']; ?>" />
      <input type="submit" name="action" value="Yes" />
    </p>
  </body>

I just want to thank everyone for looking at my post and if anyone can help me achieve what the manual is challanging that would be awesome. I also don’t like the “Yes” confirmation I created… I would like to go back to my list if nothing is deleted. I guess I really want to get as close as possible to the requirements of the challange. --Ben

What are the errors shown?

Thank you for the reply. The first error is…

Notice: Undefined variable: link in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\admin\index.php on line 86.

The second error is…

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\admin\index.php on line 86

Also, it brings up the button “Yes” which also does not work. I’m assuming the issue is with the controller at this stage (index.php). I’m trying to carry over the ID from the index page to the confirmation page, but this is not working. Thank you again for your help with this.

Also, it brings up the button “Yes” which also does not work. I’m assuming the issue is with the controller at this stage (index.php). I’m trying to carry over the ID from the index page to the confirmation page, but this is not working. Thank you again for your help with this.

Make sure you added it as “return confirm(‘message’)”.

That notice shouldn’t be much of a problem as long as $link is assigned value during the action.

For mysql error, I need to see the query to say. Mostly it’s a missed variable at WHERE clause, but need the query.

Okay, i’m getting close, but really need some help from the experts out there. I don’t have the errors anymore, however no action is taken after the comfirmation–it does not complete the cycle and delete the author. I put the three simple pages in order of the route the ID has to take in order to delete the author after confirmation. Here is the slight changes in code…

here is the body of the page (authors.php)–no changes here:


<body>
<h1>Manage Authors</h1>
  <p><a href="?add">Add new author</a></p>
  <ul>
    <?php foreach ($authors as $author): ?>
      <li>
        <form action="" method="post">
          <div>
            <?php htmlout($author['name']); ?>
              <input type="hidden" name="id" value="<?php echo $author['id']; ?>"/>
              <input type="submit" name="action" value="Edit"/>
              <input type="submit" name="action" value="Delete"/>
          </div>
        </form>
      </li>
    <?php endforeach; ?>
  </ul>
  <p><a href="..">Return to JMS home</a></p>
</body>

Here is the Controller page or author (index.php):


if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
$author['id'] = 'id';
include 'confirm.php';
exit();
}

If (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Yes’)
{

…delete code

}

header(‘Location: .’);
exit();

The portion where I have it as “…delete code” above works fine. I tested it by removing the confirmation section, and there are no problems with deleting the author.

And here is the body of the confirmation page (confirm.php) with updates:


<body>
  <form action="" method="post">
    <p>
      Are you sure you want to delete?
      <input type="hidden" name="id" value="<?php echo $id; ?>" />
      <input type="submit" name="action" value="Yes" />
    </p>
  </form>
</body>

So, here is where I need some help. When I click the “Delete” button on authors.php, it routes back to the controller page which is index.php. I need the “ID” to carry over from authors.php to index.php and back to the confirm.php page. This is where the breakdown in not getting it to delete. If I can get the “ID” to travel to the confirm.php, I should be able to click “Yes” and have it go back to the index.php and delete the author.

here is the route that the ID needs to take in order for this to work:

ID from authors.php >>> same ID to index.php >>> same ID to confirm.php >>> same ID back to index.php to delete author.

I hope that makes sense. Again thank you for reading my post and hopefully someone can help me out with this–I know that this is the long way verse JS, but I really want to understand the challenge in this book which also suggested coming to this site for help. Thanks again, --Ben

I figured it out and thought I would post my answer in case someone else came here looking for the same thing.

I changed the controller to the following…


if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
$id = mysqli_real_escape_string($link, $_POST['id']);

include 'confirm.php';
exit();
}

If (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Yes’)
{

…delete code

}

header(‘Location: .’);
exit();

I left the other two pages the same and it works. :slight_smile: