为什么当我的javascript函数返回false时,我的表单仍然提交

Why does my form still submit when my javascript function returns false?

本文关键字:我的 表单 提交 false javascript 函数 返回 为什么      更新时间:2023-09-26

这是我的Javascript表单验证器函数:

function companyName() {
    var companyName = document.forms["SRinfo"]["companyName"].value;
    if (companyName == ""){
    return false;
    } else {
    return true;
    }
}
function companyAdd() {
    var companyAdd1 = document.forms["SRinfo"]["companyAdd1"].value;
    if (companyAdd1 == ""){
    return false;
    } else {
    return true;
    }
}
function companyCity() {
    var companyCity = document.forms["SRinfo"]["companyCity"].value;
    if (companyCity == ""){
    return false;
    } else {
    return true;
    }
}
function companyZip() {
    var companyZip = document.forms["SRinfo"]["companyZip"].value;
    if (companyZip == ""){
    return false;
    } else {
    return true;
    }
}
function enteredByName() {
    var enteredByName = document.forms["SRinfo"]["enteredByName"].value;
    if (enteredByName == ""){
    return false;
    } else {
    return true;
    }
}
function dayPhArea() {
    var dayPhArea = document.forms["SRinfo"]["dayPhArea"].value;
    if (dayPhArea == ""){
    return false;
    }
}
function dayPhPre() {
    var dayPhPre = document.forms["SRinfo"]["dayPhPre"].value;
    if (dayPhPre == ""){
    return false;
    } else {
    return true;
    }
}
function dayPhSub() {
    var dayPhSub = document.forms["SRinfo"]["dayPhSub"].value;
    if (companyAdd1 == ""){
    return false;
    } else {
    return true;
    }
}
function validateForm() {
        if (companyName() && companyAdd() && companyCity() && companyZip() && enteredByName() && dayPhArea() && dayPhPre() && dayPhSub()) {
            return true;        
        } else {
            window.alert("Please make sure that all required fields are completed.");
            document.getElementByID("companyName").className = "reqInvalid";
            companyName.focus();
            return false;
        }
    }

以下是我的所有include,以防其中一个与另一个冲突(我使用jquery进行切换()):

<script type="text/javascript" src="formvalidator.js"></script>
<script type="text/javascript" src="autoTab.js"></script>
<?php 
require_once('mobile_device_detect.php');
include_once('../db/serviceDBconnector.php');
$mobile = mobile_device_detect();
if ($mobile) {
        header("Location: ../mobile/service/index.php");
    if ($_GET['promo']) {
        header("Location: ../mobile/service/index.php?promo=".$_GET['promo']);
    }
}
?>
<script src="http://code.jquery.com/jquery-1.7.1.js"></script>

这是我的表单标签,带有在Submit:上返回的函数

<form method="POST" action="index.php" name="SRinfo" onsubmit="return validateForm();">

验证工作得很好,我测试了所有字段,并不断获得适当的警报,但在警报之后,表单被提交到mysql中并作为电子邮件发送。这是我提交POST数据的代码。

if($_SERVER['REQUEST_METHOD']=='POST') {
// Here I submit to Mysql database and email form submission using php mail()

在我看来,这条线很可能会爆炸:

companyName.focus();

我看到的companyName的唯一定义是函数。不能对函数调用focus

这会爆炸,因此永远无法到达return false

我会注释掉验证部分中的所有代码,然后简单地返回false。如果这阻止了表单的过帐,则执行验证的实际代码中存在错误。每次添加一个零件,直到找到错误为止。

我的猜测和James建议的一样,你把焦点放在函数"companyName"上。上面的一行似乎试图从具有相同名称的文档中获取元素,但您没有将其分配给变量,以便对其调用焦点。