www.webdeveloper.com
Results 1 to 5 of 5

Thread: Confirm before delete

Hybrid View

  1. #1
    Join Date
    Dec 2011
    Posts
    39

    Confirm before delete

    Dear Sir, I have folloiwng codes

    PHP Code:
    if(isset($_POST['delete']))
        {
            
    // Get values from form 
            
    $sno $_POST['txtsno'];
            
    $pro $_POST['txtpro'];
            
    $wet $_POST['txtwet'];

    $query "delete from test WHERE sno={$sno};
    $result mysqli_query($con$query);
                
    if(!
    $result){
    echo 
    "<script>alert('Record not deleted')</script>";
    }else{
    echo (
    "<script>alert('Record deleted successfully')</script>"); 
    }

    It works fine, but I want to need a confirmation box like this before deleting record

    http://i42.tinypic.com/15mzc6g.jpg

    Please help

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    776
    add an onclick event to your submit button. In it point to a JS function that simply returns the result of a confim() call.

    Code:
    <input type='submit' name='delete' value='Delete' onclick='return confirmDelete()'>
    Put this in your <head> section.
    Code:
    <script type='text/javascript'>
    function confirmDelete()
    {
       return confirm("Are you sure you want to delete this?");
    }
    </script>

  3. #3
    Join Date
    Dec 2011
    Posts
    39
    Sir, I have following codes
    PHP Code:
    // DELETE 
            
    if(isset($_POST['delete'])) 
            { 
                
    // Get values from form  
                
    $sno $_POST['txtsno']; 
                
    $pro $_POST['txtpro']; 
                
    $wet $_POST['txtwet']; 
         
                    
    //$query = "delete from test WHERE sno='".$sno."'" ; 
                    
    $query "delete from ghee WHERE sno={$sno}
                    
    $result mysqli_query($con$query); 
                     
                    if(!
    $result)            { 
                    echo 
    "<script>alert('Record not deleted')</script>"
                    }else{ 
                    echo (
    "<script>alert('Record deleted successfully')</script>");  
                          } 
                } 
        
    ?> 
         
              <form name="form1" action="" method="post"> 
                <table border=0; cellpadding="1" cellspacing="1" bgcolor="" align="center" > 
                > 
                <div style=text-align:center;margin-top:20px;> 
                   
                  <input type="submit" name="delete" value="Delete" onclick="return (window.confirm('Do you really want to delete this record?') ? 1 : 0);" 
        > 
                            
                </div> 
              </form> 
    When I press delete button then confirmation apperas but..
    In both cases (Yes or No), record is deleted automaticlly.

    I think when Delete button is pressed then this part of codes runs immediately without caring Yes or No option.
    PHP Code:
     if(isset($_POST['delete'])) 
    The record must not be deleted in case NO button is pressed.

    Please help again. Thanks

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    776
    Use the code I gave you EXACTLY the way I gave it to you.

  5. #5
    Join Date
    Sep 2013
    Posts
    221
    well..., i think you must try out with the below code:
    echo"<form method = \"post\" action =\"change.php?change=$productid\">";
    echo// form fields here!...
    echo"<input type=\"submit\" name = \"delete\" value=\"Delete\" onclick=\"return deleletconfig()\" />";

    if (isset($_POST['delete'])){ //delete clicked
    //get variables here
    //run query delete record from xyz where id=$id


    }

    <script>
    function deleletconfig(){

    var del=confirm("Are you sure you want to delete this record?");
    if (del==true){
    alert ("record deleted")
    }else{
    alert("Record Not Deleted")
    }
    return del;
    }
    </script>

    I have used the above code in one of my application and it has worked fine for me.
    Hope this helps,
    strad solutionswww.stradsolutions.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles