2

My program manager is a QR-code in php. I have my list of QR-codes and have (among other options) the option to delete the QR-code. When I click delete, I want to bring up a confirmation message in javascript. When I click save, I do not need confirmation.

My form:

<form method="post" enctype="multipart/form-data">
    (...)
    <input type="submit" name="save_edit" value="SAVE" />
    <input type="submit" name="delete" value="Delete QR-code" />
</form>

My code:

if(isset($_POST['delete']))
{
    $id = $_SESSION['tmp_id'];
    $query = mysql_query("SELECT name_file FROM $tbl_query WHERE id='$id'");
    if(mysql_num_rows($query) > 0)
    {
        while($row = mysql_fetch_array($query))
        {
            $result = mysql_query("DELETE FROM $tbl_query WHERE id='$id'");
            unlink("img_qr/".$row["name_file"]);
            if($result)
            {
                $_SESSION['alert_type']=1;
                $_SESSION['msg_alerr']= "QR-code delete!";
            }
            else
            {
                $_SESSION['alert_type']=-1;
                $_SESSION['msg_alert']= "Error!";
            }
        }
    }
}

I tried javascript but it doesn't work:

<script type="text/javascript">
    function confirm() {
        var r=confirm('Are you sure you want to delete??');
        if (r==true)
        {
            //delete file...
        }   
    }
</script>

I want to see a confirmation window before deleting.

4
  • 1
    easiest way - <input type="submit" name="delete" value="Delete QR-code" onclick="confirm()" /> Commented Nov 1, 2013 at 16:46
  • 2
    No. onclick="return confirm('...')" Commented Nov 1, 2013 at 16:47
  • had tried but not worked, now work! thanks! Commented Nov 1, 2013 at 16:54
  • @pc_oc Hit there. Would you mind in accept the answer that was given IF it solved your problem? That way it will help others with similar issues. :) Commented Jul 14, 2014 at 18:23

1 Answer 1

17

Put this in your form:

<form method="post" enctype="multipart/form-data" onsubmit="return confSubmit();">

And your javascript function should be

<script type="text/javascript">
    function confSubmit() {
       var r=confirm('Are you sure you want to delete??');
       return r;
    }
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

Onsubmit is a good idea, however in this case there is likely another submit too
but i have more "submits", for example, save
Then you have to create some action value and testit in your javascript function. You asked just for the delete confirmation. Improve your question then I will complete my answer
It was pretty clear there would be more than one submit :) although I never recommend onclick of a submit, I think in this case it would be the way to do it
I do not see where objectivity is compromised when thinking with the OP instead of only going with what is already posted
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.