简体   繁体   English

Javascript不会停止php表单提交

[英]Javascript won't stop php form submission

Javascript gives error window, but it won't stop the form submission. Javascript提供了错误窗口,但不会停止表单提交。 I'm stumped at this point and can't find an exact answer. 在这一点上我很困惑,找不到确切的答案。 Here is the code, thanks: 这是代码,谢谢:

    function newNameValidate() {
        var x = document.forms["checkIn"]["newName"].value;
        if (x==null || x=="") {
        alert("Tech name must be filled out");
        return false;
    }
        return true;
    }

Here is the HTML: 这是HTML:

<form name="checkIn" onsubmit="newNameValidate(checkIn)" action="check_in_complete.php" method="POST">

<input type ="submit" class="input" value="CHECK IN">

您必须返回函数的值onclick =“ return foo();”

As others have said, you need to return the value returned from the function, but a better option is to use unobtrusive JavaScript: 正如其他人所说,您需要返回从函数返回的值,但是更好的选择是使用不引人注目的JavaScript:

<form name="checkIn" action="check_in_complete.php" method="POST">

Script: 脚本:

window.onload = function() { 
    document.forms["checkIn"].onsubmit = newNameValidate;
}
It can be done like: 
<form name="checkIn" id="checkIn" action="check_in_complete.php" method="POST">
<input type ="button" class="input" value="CHECK IN"  onclick="newNameValidate()">
</form>

<script>
function newNameValidate() {
        var x = document.forms["checkIn"]["newName"].value;
        if (x==null || x=="") {
        alert("Tech name must be filled out");
        return false;
    }else{
            checkIn.submit();
        }
    }
</script>

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

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