0

Can someone explain why the following PHP form will not work. I have tried submitting a form, but the inbox does not receive the email. It is the correct email address.

Moreover, would it be better to use PHP instead of jQuery to alert "Your message has been successfully sent!"? Because at the moment it states the message was sent even though the inbox is not receiving it. If so, how?

HTML:

<div id="formDiv">
        <form id="validationForm">

            <label for="anti" id="antiLabel">This is an anti 's'-to-the-'pam' field. Accordingly, do not delete or enter any information here.</label>
            <input name="anti" id="antiInput"/> 

        <div id="formSection1">
            <label for="name" class="label">Name:</label>
            <input name="name" id="name" class="input" required/> 

            <label for="email" class="label">Email:</label>
            <input name="email" id="email" class="input" required/> 

            <label for="subject" class="label">Subject:</label>
            <input name="subject" id="subject" class="input" required/> 
        </div>

        <div id="formSection2">

            <label for="message" id="labelMessage">Message:</label>
            <textarea name="message" id="inputMessage" required></textarea>

            <input id="submitButton" type="submit" value="Submit" class="input"/>
        </div>

        </form>
    </div>

PHP:

<?php 
$ToEmail = 'jono@s.co.uk'; 
$EmailSubject = 'Site contact form'; 

$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 

$MESSAGE_BODY = "Name: ".$_POST["name"].""; 
$MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
$MESSAGE_BODY .= "Subject: ".nl2br($_POST["subject"]).""; 
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"]).""; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?>

jQuery:

<script>

$("#validationForm").submit(function(event){

        var errorMessage="";

        event.preventDefault();

        function isValidEmailAddress(emailAddress) {
        var pattern = new 
        RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return pattern.test(emailAddress);
        };

            /* Regex validation of email address */

        if (!isValidEmailAddress($("#email").val())) {
            errorMessage="*Please enter a valid email address*";}


        if (errorMessage=="") {
            $("#error").html("");
            alert("Your message has been successfully sent!");
        } else {
            $("#error").html(errorMessage);
        }

        if(!String.IsNullOrEmpty(Request.Form["anti"]))
            IgnoreComment();

        });


    </script>
JonnyBoy
  • 61
  • 1
  • 8

2 Answers2

1
 <form id="validationForm"> 

where have you defined the action?

Add method and action please

 <form id="validationForm" action="yourAction.php" method="post" >

If you want to post it there, just add the method='post'

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
1

If you plan on retrieving your form values with $_POST['myField'], you need to add the proper method attribute to your <form> tag.

Hence, use:

<form id="validationForm" method="post">

Because it is get by default.

D4V1D
  • 5,805
  • 3
  • 30
  • 65