如何配置提交按钮,使其可以提交已验证的表单?

时间:2019-12-20 08:56:53

标签: javascript html validation submit

我遇到的问题是,一旦在“提交”按钮中添加了“ return validate()”,该按钮将无法提交。但是验证工作正常。无论如何,有没有绕过这个方法,以便javascript可以验证我的文件然后提交表格?

这是我的javascript:

function validate()
{

    var username = document.getElementById("name");
    var img = document.getElementById("image");


    if(username.value.trim()=="")
    {
        var myDiv = document.getElementById("error");
        myDiv.innerHTML = "Username Not Entered";
    }

    else if (img.value.trim()=="")

    {
        var myDiv = document.getElementById("error");
        myDiv.innerHTML = "Please Select an image";
    }

    return false;


}


</script>

这是我的html,当我将return放在

        <div class="add-form">
            <h1 class="text-center"><font face="fantasy">Please Insert new image</font></h1>
            <form onSubmit = "validate()" method="post" enctype="multipart/form-data" id="FormID">
                <label>User Name</label>
                <input id = "name" type="text" name="user_name" class="form-control">
                <label>Select image to upload</label>
                <input id = "image" type="file"  onchange ="unlock()" name="profile" class="form-control2" accept="*/image">
                <button type="submit" value = "submit" name="btn-add">upload</button>
                <div id = "error">Please fill up the username and select and image</div>

            </form>
        </div>
        <hr style="border-bottom: 5px blue solid;">
    </div> 
<!-- end form insert -->
<!-- view form -->
<div class="container">
    <div class="view-form">
        <div class="row">
        <?php 
            $stmt=$db_conn->prepare('SELECT * FROM tbl_user ORDER BY id DESC');
                $stmt->execute();
                if($stmt->rowCount()>0)
                {
                    while($row=$stmt->fetch(PDO::FETCH_ASSOC))
                    {
                        extract($row);
                        ?>

            <div class="col-sm-3">
            <img src="uploads/<?php echo $row['picprofile']?>"><br><br>
            <a class="btn btn-info" href="edit_form.php?edit_id=<?php echo $row['id']?>" title="click for edit" onlick="return confirm('Sure to edit this record')"><span class="glyphicon glyphicone-edit"></span>Edit</a>
            <a class="btn btn-danger" href="?delete_id=<?php echo $row['id']?>" title="click for delete">Delete</a>

            </div>

            <?php 

                }
            }
            ?>
        </div>

    </div>
</div>
<!-- end view form -->
<script type="text/javascript" src="js/bootstrap.min.js"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您可以在validate函数内创建变量,并仅在有效数据的情况下提交表单:

function validate()
{
    var username = document.getElementById("name");
    var img = document.getElementById("image");
    var myDiv = document.getElementById("error");

    let valid = true;

    if(username.value.trim()=="")
    {
        myDiv.innerHTML = "Username Not Entered";

        valid = false;
    }
    else if (img.value.trim()=="")
    {
        myDiv.innerHTML = "Please Select an image";

        valid = false;
    }

    if(valid)
    {
        document.getElementById('FormID').submit();
    }

    return false;
}

还将<'form<button行更改为此:

<form method="post" enctype="multipart/form-data" id="FormID">
...
<button type="submit" value = "submit" name="btn-add" onClick = "validate()">upload</button>

答案 1 :(得分:0)

您应该尝试:

<form onSubmit = "return validate()"

在以下情况下,您还需要包装return false

function validate()
{

    var username = document.getElementById("name");
    var img = document.getElementById("image");


    if(username.value.trim()=="") {
        var myDiv = document.getElementById("error");
        myDiv.innerHTML = "Username Not Entered";
        return false;
    }
    else if (img.value.trim()=="") {
        var myDiv = document.getElementById("error");
        myDiv.innerHTML = "Please Select an image";
        return false;
    }

}