Contact form won't send email - keeps "loading"

For some reason, my contact form won’t send an email. I have some validation that doesn’t work now currently either. I keep getting an error on line 11 of the PHP page - where I have “.n\n”

Here is the Contact.html page

<div class="form-container">
            <form name="contact-form" id="contact-form" method="POST" action="Contact.php" onsubmit="return validateForm()">
              <div class="input-row">
                <label for="name" class="labelControl" style="padding-top: 20px;">Name</label>
                <input type="text"
					class="formControl" name="name" id="name">
              </div>
              <div class="input-row">
                <label for="email" class="labelControl">Email</label>
                <input type="text" class="formControl" name="email"
					id="email">
              </div>
              <div class="input-row">
                <label for="subject" class="labelControl">Subject</label>
                <input type="text" class="formControl" name="subject" id="subject">
              </div>
              <div class="input-row">
                <label for="message" class="labelControl">Message</label>
                <textarea name="message" id="message" class="formControl" cols="60"
					rows="6"></textarea>
              </div>
              <div class="pb-2"> <a class="btn btn-primary mb-2" onclick="validateForm();">Send</a>
                <div class="status"></div>
              </div>
            </form>
          </div>

The javascript is on the html page

<script>
function validateForm() {
    var x =  document.getElementById('name').value;
     if (x == "") {
        document.getElementById('status').innerHTML = "Name cannot be empty";
         return false;
     }
     x =  document.getElementById('email').value;
     if (x == "") {
        document.getElementById('status').innerHTML = "Email cannot be empty";
         return false;
     } else {
        var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
         if(!re.test(x)){
            document.getElementById('status').innerHTML = "Email format invalid";
             return false;
         }
     }
     x =  document.getElementById('subject').value;
     if (x == "") {
         document.getElementById('status').innerHTML = "Subject cannot be empty";
         return false;
     }
     x =  document.getElementById('message').value;
     if (x == "") {
         document.getElementById('status').innerHTML = "Message cannot be empty";
        return false;
     }
	document.querySelector('.status').innerHTML = "Sending...";
}
 //get input field values data to be sent to server
	
document.getElementById('status').innerHTML = "Sending...";
formData = {
  'name': $('input[name=name]').val(),
  'email': $('input[name=email]').val(),
  'subject': $('input[name=subject]').val(),
  'message': $('textarea[name=message]').val()
};


$.ajax({
  url: "Contact.php",
  type: "POST",
  data: formData,
  success: function (data, textStatus, jqXHR) {

    $('#status').text(data.message);
    if (data.code) //If mail was sent successfully, reset the form.
      $('#contact-form').closest('form').find("input[type=text], textarea").val("");
  },
  error: function (jqXHR, textStatus, errorThrown) {
    $('#status').text(jqXHR);
  }
});
 </script>

Here is the Contact.php page

<?php
  
if (isset($_POST['submit'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $subject = $_POST['subject'];
 
  $mailTo = "[email protected]";
  $headers = "From: ".$mailFrom;
  $txt = "You have a message ".$name".\n\n".$message;

  mail($mailTo, $name, $txt, $headers);

  header("Location: index.html?MessageSent");
}
?>

This looks like a test question - list all the ways this code can be improved or fixed? Okay -

  1. If you put <label></label> tags around the field they go with, you can eliminate all the for=’…’ and corresponding id=’…’ attributes.
  2. There’s no id=‘status’ in the markup, so all the getElementById(‘status’) references don’t work and produce errors in the browser’s developer console.
  3. All modern browsers can perform this client-side validation. Just add the ‘required’ keyword to each field and change the type of the email field to type=‘email’
  4. The javascript getting the form data and sending it via ajax is being unconditionally executed when the page loads. It would need to be inside the validateForm() { function definition for it to only be executed when the ‘Send’ button is clicked.
  5. You can just use a javascript formData object to get all the successful form field values at once, without writing out a line of code for each value.
  6. The server-side code doesn’t output any value back to the ajax code, so the success section won’t do what you think.
  7. Even in the best of times, you should NOT attempt to detect if the submit button is set in the server-side code. There are cases where it won’t be and since you are using ajax to submit the form, this is one of those cases. Instead, detect if a post method form was submitted.
  8. Do not copy variables to other variables for nothing. This is just a waste of typing time. Instead, keep the form data as a set in a php array variable, then operate on elements in this array variable throughout the rest of the code.
  9. Data submitted to your server can come from anywhere, not just your code, can be anything, and cannot be trusted. You MUST trim, then validate all inputs before using them.
  10. Validate each input separately, storing validation errors in an array using the field name as the array index.
  11. After the end of all the validation logic, if there are no errors (the array will be empty), use the form data.
  12. Apply htmlentities() to all values being used in a html context, when you output them, to help prevent cross site scripting.
  13. The code isn’t even using all the form fields.
  14. After validating that the submitted email address is only and exactly one validly formatted email address, you can put it into a Reply-to: mail header.
  15. Don’t put external data into the mail() ‘subject’ parameter. It’s not clear what if any protection against mail header injection php provides.
  16. The error you are getting is a php syntax error, because there are not matching opening and closing double-quotes around the string.
  17. You must have error handling for statements that can fail. You must test for a false value returned from the mail() call, setup a general failure message for the visitor, and log all the actual information about the error.
  18. If you were submitting a form directly to the server-side code, upon successful completion you would perform a redirect to the exact same url of the current page to cause a get request for that page and every redirect needs an exit/die statement to stop code execution. However, since you are making an ajax request to this code, there’s no point in having a redirect, since it won’t do anything.
  19. Since you are making an ajax request to this code, you need to build, json encode, and output an array with either a success message or failure messages (consisting of the validation errors and any mail() failure message.)
Sponsor our Newsletter | Privacy Policy | Terms of Service