0

I thought I had this problems solved, but my issue is still happening.

Every time I click send, my form still sends. I thought I had my JavaScript correctly validating the form, but it's not working because I can put anything in the email fields, and it still processes. The alerts still work. I just want to use PHP and JavaScript, no AJAX or anything. Here is my code:

<?php

$to = '[email protected]';
$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>             
3
  • Could u post form HTML please. Thanks Commented Oct 6, 2013 at 2:18
  • I did today! Sorry for the delay! Commented Oct 7, 2013 at 5:40
  • Thanks! I have put an answer below! I hope it helps? Commented Oct 8, 2013 at 1:47

4 Answers 4

1

I guess this is what you are looking for:

<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;}


}

And call this function like this:

<form name="a" method="post" onSubmit="return confirmEmailAddresses()" action="xyz.html" id="a">
Sign up to request clarification or add additional context in comments.

11 Comments

And it still sends blank forms if not filled out at all.
@user2638775 where exactly are you calling "confirmEmailAddresses()" ?
The call is in the JavaScript and then it is attached to the "Submit" button.
try the code i have posted, you just have to change the name of the form in yur code, as in my code the name of the form is "a"
@user2638775: do let me know if you face any issues using my code. I'll be available after 6 hrs from now ")
|
1

Try removing the onClick event from the submit button, and add return confirmEmailAddresses(); to the onsubmit event of the main form element.

Make sure that the confirmEmailAddresses() function has the line return true; if the validation passes! Else return false;.

2 Comments

I've done all of this with no success. :( I appreciate your help though. This is the code I have: function confirmEmailAddresses(){ if (checkforblank() == true) { if (verifyEmail() == true) { document.getElementById("myForm").submit(); return false; } } } I also have: function confirmEmailAddresses(){ if((checkforblank())&&(verifyEmail())){return true;}else{return false;}
Instead of "document.getElementById("myForm").submit();" try just using "return true;". If you make the function call "onsubmit" as described above, ensuring it says "return confirmEmailAddress();" the form is now expecting true or false to be returned. Sorry i overlooked this before!
0

You might try

return false; 

after each of your alert() calls.

2 Comments

Thank you for the tip! I shall try it. Just some clarification...after each of my alert calls? The form submits fine in Firefox and Chrome, but not in Safari.
So I tried "return false" but it makes it not work in any browser now. Any other suggestions? I do appreciate the help!
0

Isn't that what you want? If the form is not valid, you want to show the alert and not submit the form, right?

It might help to see the html form where you're calling confirmEmailAddresses()

1 Comment

The invalid form is sending in Safari only. It's quite odd! I appreciate the help! I'm still working on it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.