...

View Full Version : Confirm box not deleting row even after pressing "OK"



anita_86
03-05-2012, 10:15 AM
Hi all.I found this code on a forum to confirm that the user wants to delete the record from database and that the link has not pressed accidently.But it is not deleting the record even after pressing ok.This is may be because the id is not passed properly. Can anyone tell why?Do I need to modify the javascript function?Here is my code:


function confirmation() {
var answer = confirm("Delete Record?")
if (!answer){

window.location = "by_hand.php";
}
else
{
window.location = "delete.php?delete=$id&action=deleteemployee";
}
}


<a href=# onclick='confirmation()'><img src='images/delete.png' alt='Delete' title='Delete' class='pngfix'></a>

webdev1958
03-05-2012, 10:38 AM
From your posted code it looks like you don't have much of an idea about what is going on. delete.php is most probably the script that deletes the record and so the problem is probably in that code.

Also, alert() the url to delete.php your javascript is generating and I think you'll find the id parameter in the query string is not set correctly.

What is $id.

anita_86
03-06-2012, 01:18 PM
Actually im displaying total records from the database and when user clicks on certain record, I want a confirmation box that user havent clicked the button accidentally.So $id is the id of that particular record.And through href the
delete.php?delete=$id&action=deleteemployee link works fine.Just dont know how to do it through js.If I alert $id, it displays just $id and not the value.

webdev1958
03-06-2012, 01:44 PM
You could set up your delete links for each record along the lines of this demo.

As you loop through your db records, you create a 'delete' link for each record for each record id.

Normally you would use check boxes for each record so you can delete multiple records in one go, but probably best to keep it simple for now.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css"></style>
<script type="text/javascript">
function deleteRecord(recID){
var ans = window.confirm('Are you sure you want to delete record '+recID);
if(ans == true){
window.location.href='delRecord.php?id='+recID;
}
}
</script>
</head>
<body>
<?php
$id = 'qw1'; //this line is just for testing purposes

// this echo would be in your loop
echo '<a href="" onclick="deleteRecord('."'" . $id . "'" .');return false;">Delete</a>';

?>
</body>
</html>

anita_86
03-06-2012, 02:45 PM
Thanks for your valuable reply webdev1958.It was very helpful and it solved the issue.
My new code is

<script type="text/javascript">
function deleteRecord(recID){
var ans = window.confirm('Delete Record?');
if(ans == true){
window.location.href='delete.php?delete='+recID+'&action=deleteemployee';
}
}
</script>
$id=$row['eng_id'];
echo '<a href="" onclick="deleteRecord('."'" . $id . "'" .');return false;"><img src="images/delete.png" alt="Delete" title="Delete" class="pngfix"></a>';

webdev1958
03-06-2012, 09:58 PM
Thanks for your valuable reply webdev1958.It was very helpful and it solved the issue.


good to see you sorted it out :thumbsup:

If you wanted to make the table of records a little more user friendly, then instead of having a delete link for each record you could have a checkbox for each record instead of the link and so the user can check the boxes for the records they want to delete. The user then clicks a 'Delete selected records' button which then pops up a confirmation prompt. If the user confirms they want to delete the records, the array of selected checkbox values containing the id's to delete is sent to delete.php.

anita_86
03-07-2012, 07:39 AM
Ok.Yes it will be more user friendly.I would like to know how to pass 2 values through
onclick="deleteRecord('."'" . $id . "'" .');
Because I want to pass $number to delete.php and
onclick="deleteRecord('."'" . $id,$number . "'" .'); is not working.So I guess it is not the correct way to pass values in js :o

webdev1958
03-07-2012, 09:40 AM
Have a look at the html the echo creates with your


onclick="deleteRecord('."'" . $id,$number . "'" .');

You should be able to spot the syntax error in the created html and then fix the php code so that it creates the correct html.

anita_86
03-07-2012, 03:55 PM
Thanks again webdev1958 :)
Finally I sorted it out.Now code is:


function deleteRecord(recID,no){

var ans = window.confirm('Delete Record?');
if(ans == true){
window.location.href="delete.php?delete=" +
recID +
"&action=deletesale&number=" + no;
}
}
echo '<a href="" onclick="deleteRecord('."'" . $id."'" .",". "'" .$number . "'" .');return false;"><img src="images/delete.png" alt="Delete" title="Delete" class="pngfix"></a>';



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum