Confirm Request with messagebox before running a delete query?

Hello PHP Buddies,

I used this simple php codes to have a section on my page to delete records.
[php]
<a href=’?cmd=delete&ID={$rows[‘ID’]}’>Delete
[/php]
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]
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:");
}

}
[/php]

So I know this done with javascript but my javascript knowledge stinks

I added

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

<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

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:

<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]if(isset($_GET[‘delnews’])){
//run delete based on id inside $_GET[‘delnews’]
}
[/php]
[php]Delete[/php]

just what i needed it
thank you very much.

works like a charm

Sponsor our Newsletter | Privacy Policy | Terms of Service