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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19731265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:38:12  来源:igfitidea点击:

before form submit call function javascript

javascriptphp

提问by pc_oc

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.

我的程序管理器是 php 中的二维码。我有我的二维码列表,并且有(除其他选项外)删除二维码的选项。当我单击删除时,我想在 javascript 中显示一条确认消息。当我点击保存时,我不需要确认。

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.

我想在删除之前看到一个确认窗口。

回答by Jorge Campos

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>