简体   繁体   English

在表单提交调用函数javascript之前

[英]before form submit call function javascript

My program manager is a QR-code in php. 我的程序经理是php中的QR码。 I have my list of QR-codes and have (among other options) the option to delete the QR-code. 我有我的QR码列表,并有(以及其他选项)删除QR码的选项。 When I click delete, I want to bring up a confirmation message in javascript. 当我点击删除时,我想在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: 我试过javascript但它不起作用:

<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. 我希望在删除之前看到确认窗口。

Put this in your form: 把它放在你的表格中:

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

And your javascript function should be 你的javascript函数应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM