Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Confirm box not deleting row even after pressing "OK"

    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:
    Code:
    function confirmation() {
    	var answer = confirm("Delete Record?")
    	if (!answer){
    		
    		window.location = "by_hand.php";
    		}
             else
             {
                   window.location = "delete.php?delete=$id&action=deleteemployee";
              }
    }
    PHP Code:
    <a href=# onclick='confirmation()'><img src='images/delete.png' alt='Delete' title='Delete' class='pngfix'></a> 

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    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.
    Last edited by webdev1958; 03-05-2012 at 09:43 AM.

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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
    Code:
    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.

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    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.

    Code:
    <!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>
    Last edited by webdev1958; 03-06-2012 at 12:47 PM.

  • Users who have thanked webdev1958 for this post:

    anita_86 (03-06-2012)

  • #5
    New Coder
    Join Date
    Mar 2011
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for your valuable reply webdev1958.It was very helpful and it solved the issue.
    My new code is
    Code:
     <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>';

  • #6
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by anita_86 View Post
    Thanks for your valuable reply webdev1958.It was very helpful and it solved the issue.
    good to see you sorted it out

    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.

  • #7
    New Coder
    Join Date
    Mar 2011
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok.Yes it will be more user friendly.I would like to know how to pass 2 values through
    Code:
    onclick="deleteRecord('."'" . $id . "'" .');
    Because I want to pass $number to delete.php and
    Code:
    onclick="deleteRecord('."'" . $id,$number . "'" .');
    is not working.So I guess it is not the correct way to pass values in js

  • #8
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Have a look at the html the echo creates with your

    Code:
    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.

  • #9
    New Coder
    Join Date
    Mar 2011
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks again webdev1958
    Finally I sorted it out.Now code is:
    Code:
    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>';


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •