Jump to content

[SOLVED] Confirmation upon delete


`Karl

Recommended Posts

What I want to do is have a confirmation page between the deletion of the MySQL record, I want to have buttons, one saying "Yes" one saying "No". No will redirect to the previous page and Yes will continue with the deletion of the record, by the ID selected in editskill.php.

 

But I can't figure out how to do this without JavaScript and without making another 20+ files for each different page I have. Because I have editnews.php as well plus others which I want to be able to delete with a confirmation.

 

If anyone could help I'd appreciate it.

 

I have editskill.php

<th width="100%"><left><font color="#FFFFFF" size="1"><b><a href="editskill.php?id={$arr['id']}">{$arr['guidename']}</a></b></font></left>
<th><font color="#FFFFFF" size="1"><b><form action="deleteskill.php" method="post">
<input type="hidden" name="id" value="{$arr['id']}">
<input type="Submit" value="Delete">
</form>

 

I also have deleteskill.php

<?php
$con = mysql_connect("*");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("*", $con);

$sql="DELETE FROM skills WHERE `id` = '$_POST[id]'";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<font color='#FFFFFF' size='2'>1 record deleted</font>";

mysql_close($con)
?> 

Link to comment
Share on other sites

without making another 20+ files for each different page I have

 

The first step to achieve that is to make the form and the form processing code be part of the same page. The second step is to incorporate the form/form processing code as a 'unit' into any page that needs to use it. See the example code posted at this link - http://www.phpfreaks.com/forums/index.php/topic,269340.msg1271304.html#msg1271304 for how you might do this (you don't need to use functions/OOP, but it helps.)

 

Basically, by using an ?action= parameter on the end of the URL that gets changed depending on which 'step' you are at, you can modify the code posted at that link to let you do anything in the order that you want it.

Link to comment
Share on other sites

And for the example code you posted, here is a basic framework you could start with -

<?php
// basic code to do the confirmation and deletion when the 'delete' button for any item is selected

$url_action = isset($_GET['action']) ? strtolower($_GET['action']) : '';
$url_id = isset($_GET['id']) ? strtolower($_GET['id']) : '';

// process any get action=
switch ($url_action){

case 'confirm':
$content = <<< EOT
Delete Record with id: {$url_id} ? <form action="?action=delete&id={$url_id}" method="post">
<input type="Submit" name="delete" value="Yes"><input type="Submit" name="delete" value="No">
</form>
EOT;
break;

case 'delete':
	if($_POST['delete'] == 'Yes'){
		// your code to actually delete the record
		$content = "You have deleted the record with id = {$url_id}<br />";
	} else {
		$content = "You picked No<br />";
	}
break;

default:
}
  
echo $content;

// display the edit/delete links/forms -
$arr['id'] = 123; // dummy id for demo
echo <<< EOT
What do you want to do with the record with id: {$arr['id']} <form action="?action=confirm&id={$arr['id']}" method="post">
<input type="Submit" name="submit" value="Delete">
</form>
EOT;
$arr['id'] = 124; // dummy id for demo
echo <<< EOT
What do you want to do with the record with id: {$arr['id']} <form action="?action=confirm&id={$arr['id']}" method="post">
<input type="Submit" name="submit" value="Delete">
</form>
EOT;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.