Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

php - before form submit call function javascript

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...