Author Topic: Confirm Request with messagebox before running a delete query?  (Read 643 times)

wilson382

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 649
  • Karma: 27
  • PHP Helper
    • View Profile
Hello PHP Buddies,

I used this simple php codes to have a section on my page to delete records.
PHP Code: [Select]

<a href='?cmd=delete&ID={$rows['ID']}'>Delete</a>

 working fine.

I want to add an action so that i when i click delete i have to confirm first.
Right now when i click it deletes the record right away. I dont want that i might misclick and lose data acidentically.

PHP Code: [Select]

if ((isset($_GET['cmd'])) && (isset($_GET['ID'])))
{
include(
'../../connect_db.php');
//$header = $_SESSION[md5("search")];

	
if
	
(
$_GET['cmd'] == "delete")
	
{
    
mysql_query("DELETE FROM `programming_language_snippet_codes_vaults` WHERE `ID` ='".intval($_GET['ID'])."'") or die("Failed to delete Record with ID: ".$_GET['ID']."<br/>".mysql_error());
	
//header("location: $header") or die("Failed to delete Record with ID:");
	
}
	

}


So I know this done with javascript but my javascript knowledge stinks
If help you, Click the [+] Karma to thanks me

wilson382

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 649
  • Karma: 27
  • PHP Helper
    • View Profile
Re: Confirm Request with messagebox before running a delete query?
« Reply #1 on: August 24, 2012, 01:44:39 AM »
I added

PHP Code: [Select]

<a href='?cmd=delete&ID={$rows['ID']}' onClick='onDelete()'>Delete</a>


Code: [Select]
<script language="JavaScript">
function onDelete()
{
if(confirm('Are you dure you want to delete this record?')==true)
{
return true;
}
else
{
return false;
}
}
</script>

no matter if i click cancel it still go ahead and delete the record LOL
If help you, Click the [+] Karma to thanks me

daveismyname

  • Senior Member
  • ****
  • Posts: 301
  • Karma: 6
  • PHP Helper
    • View Profile
    • PHP Help Tutorials
Re: Confirm Request with messagebox before running a delete query?
« Reply #2 on: August 24, 2012, 03:13:45 AM »
ah in a nutshell you need javascript to confirm to delete or not cancel should not do anything but pressing ok should then call a function to delete the record I've written a tutorial on this http://www.phphelptutorials.com/tutorials/php-tutorials/delete-rows-from-a-mysql-database-with-a-confirmation/

In a nutshell, you have a javacript function in the header of the HTML:

Code: [Select]
<script language="JavaScript" type="text/javascript">
function delnews(newsID, newsTitle)
{
if (confirm("Are you sure you want to delete '" + newsTitle + "'"))
{
    window.location.href = 'admin.php?delnews=' + newsID;
}
}
</script>

then create a delete link that calls that function passing in an id and a title the title is used in the prompt the id is then passed in the event the delete needs to be done.

then in this example on admin.php there would be a bit of code looking for delnews:

PHP Code: [Select]
if(isset($_GET['delnews'])){
 
//run delete based on id inside $_GET['delnews']
}

PHP Code: [Select]
<a href="javascript:delnews('$row->newsID','$row->newsTitle')">Delete</a>

wilson382

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 649
  • Karma: 27
  • PHP Helper
    • View Profile
Re: Confirm Request with messagebox before running a delete query?
« Reply #3 on: August 24, 2012, 01:21:39 PM »
just what i needed it
thank you very much.

works like a charm
If help you, Click the [+] Karma to thanks me