0

我以为我已经解决了这个问题,但我的问题仍然存在。

每次单击发送时,我的表单仍然发送。我以为我的 JavaScript 正确验证了表单,但它不起作用,因为我可以在电子邮件字段中放入任何内容,并且它仍在处理。警报仍然有效。我只想使用 PHP 和 JavaScript,没有 AJAX 或任何东西。这是我的代码:

<?php

$to = 'email@email.com';
$subject = 'Voice4Autism Inquiry';

$FirstName = $_POST['fname'];
$LastName = $_POST['lname'];
$eMail = $_POST['email'];

$message = <<<EMAIL

Hi!<br /><br/>

My name is $FirstName $LastName.  I am interseted in your newsletter from Voice4Autism.  Please add $eMail to your listserve.<br /><br />

Thank you,<br />
$FirstName $LastName

EMAIL;

$header = "From: $eMail\r\n";
$header = "Content-type: text/html\r\n";

if($_POST){
    mail($to, $subject, $message, $header);
}
?>

JAVASCRIPT

<script type="text/javascript">
function checkforblank() {
    var errormessage = "";

    if (document.getElementById('fname').value ==""){
        errormessage += "enter your first name \n";
    }
    if (document.getElementById('lname').value ==""){
        errormessage += "enter your last name \n";
    }
    if (document.getElementById('email').value ==""){
        errormessage += "enter your email \n";
    }
    if (document.getElementById('confirmEmail').value ==""){
        errormessage += "confirm your email \n";
    }

    if (errormessage != ""){
        alert(errormessage);
        return false;
    } else return true;
}

function verifyEmail() {
    var status = false;     
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

    if (document.myForm.email.value.search(emailRegEx) == -1) {
              alert("Please enter a valid email address.");
              return false;
    }


    if (document.getElementById('email').value == document.getElementById('confirmEmail').value) {
        alert("Thank you for your interest!");
            return true;              
    } else {
        alert("Email addresses do not match.  Please retype them to make sure they are the same.");
        return false;
    }

    return status;
}

function confirmEmailAddresses(){
    if (checkforblank() == true) {
        if (verifyEmail() == true) {
            document.getElementById("myForm").submit();
        }
    } 
}
</script>

HTML

<form name="myForm" action="contact.php" method="post" enctype="application/x-www-form-urlencoded" onsubmit="">
<table width="377" height="96" align="center">
  <tr>
    <td style="text-align: right">First Name:</td>
    <td><label for="FirstName"></label>
      <input type="text" name="fname" id="fname"></td>
  </tr>
  <tr>
    <td style="text-align: right">Last Name:</td>
    <td><label for="LastName"></label>
      <input type="text" name="lname" id="lname"></td>
  </tr>
  <tr>
    <td style="text-align: right">E-mail:</td>
    <td><input type="email" name="email" id="email"></td>
  </tr>
  <tr>
    <td style="text-align: right">Confirm E-mail:</td>
    <td><input type="email" name="confirmEmail" id="confirmEmail"></td>
  </tr>
</table>
<p align="center"><input type="submit" value="Send" onClick="confirmEmailAddresses()"><input type="reset" value="Reset Form"></p>

</form>             
4

4 回答 4

1

我想这就是你要找的东西:

<script>
function checkforblank() {
    var errormessage = "";

    if (document.getElementById('fname').value ===""){
        errormessage += "enter your first name \n";
    }
    if (document.getElementById('lname').value ===""){
        errormessage += "enter your last name \n";
    }
    if (document.getElementById('email').value ===""){
        errormessage += "enter your email \n";
    }
    if (document.getElementById('confirmEmail').value ===""){
        errormessage += "confirm your email \n";
    }

    if (errormessage !== ""){
        alert(errormessage);
        return false;
    } 
    else{return true;}
}

function verifyEmail() {
    var status = false;     
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    var email_reg = document.getElementById('email').value;

    if (!email_reg.match(emailRegEx)) {
              alert("Please enter a valid email address.");
              return false;
    }


    else if (document.getElementById('email').value !== document.getElementById('confirmEmail').value) {
        alert("Email addresses do not match.  Please retype them to make sure they are the same.");
            status = false;            
    } else {
        alert("Thank you for your interest!");
        status = true;
    }

    return status;
}

function confirmEmailAddresses(){
    if((checkforblank())&&(verifyEmail())){return true;}else{return false;}


}

并像这样调用这个函数:

<form name="a" method="post" onSubmit="return confirmEmailAddresses()" action="xyz.html" id="a">
于 2013-10-06T02:45:07.573 回答
1

尝试onClick从提交按钮中删除事件,并添加return confirmEmailAddresses();onsubmit主表单元素的事件中。

如果验证通过,请确保该confirmEmailAddresses()函数具有该行!return true;否则return false;

于 2013-10-08T01:46:26.453 回答
0

你可以试试

return false; 

在您的每个 alert() 调用之后。

于 2013-10-06T02:26:14.713 回答
0

这不是你想要的吗?如果表单无效,您希望显示警报而不提交表单,对吗?

查看您调用 confirmEmailAddresses() 的 html 表单可能会有所帮助

于 2013-10-06T02:45:50.047 回答