0

I'm just a beginner and trying to implement ajax contact form with php mailing script. But when I click on Submit nothing happens and nothing appears.

Below is my codes.

HTML

<form id="contactform" class="contact-form text-center" role="form">
    <!-- IF MAIL SENT SUCCESSFULLY -->
    <h6 class="success">
        <span class="olored-text icon_check"></span> Your message has been sent successfully.</h6>

    <!-- IF MAIL SENDING UNSUCCESSFULL -->
    <h6 class="error">
        <span class="colored-text icon_error-circle_alt"></span> E-mail must be valid.</h6>

    <input id="cf-name" type="text" name="cf-name" placeholder="Your Name">
    <input id="cf-email" type="email" name="cf-email" placeholder="Your Email">
    <input id="cf-address" type="text" rows="7" name="cf-address" placeholder="Your Home Address">
    <input id="cf-phone" type="text" rows="7" name="cf-phone" placeholder="Your Phone Number">
    <input type="submit" class="button alt" value="Submit" id="submit" name="submit" data-style="expand-left"/>
</form>

Javascript

// Function for email address validation
function isValidEmail(emailAddress) {

    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

    return pattern.test(emailAddress);

}
/* =================================
 CONTACT FORM
 =================================== */
$("#contactform").submit(function (e) {
    e.preventDefault();
    var name = $("#cf-name").val();
    var email = $("#cf-email").val();
    var address = $("#cf-address").val();
    var message = $("#cf-phone").val();
    var dataString = 'name=' + name + '&email=' + email + '&address=' + address + '&phone=' + message;

    function isValidEmail(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);
    }
    if (isValidEmail(email) && (message.length > 1) && (address.length > 1) && (name.length > 1)) {
        $.ajax({
            type: "POST",
            url: "sendmail.php",
            data: dataString,
            success: function () {
                $('.success').fadeIn(1000);
                $('.error').fadeOut(500);
            }
        });
    }
    else {
        $('.error').fadeIn(1000);
        $('.success').fadeOut(500);
    }
    return false;
});

PHP

<?php
// Get values from jquery
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];

$to = "name@email.com";
$subject = "New Email Subscriber";
$message = " Name: " . $name . "\r\n\r\n Email: " . $email . "\r\n\r\n Address: " . $address . "\r\n\r\n Phone: " . $phone;


$from = "ContactForm";
$headers = "From:" . $from . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n"; 

if (@mail($to, $subject, $message, $headers)) {
    echo "success";
} else {
    echo "invalid";
}

Can someone please fix this code? I don't what's wrong with it.

Thanks in advance.

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
Hasan Rony
  • 69
  • 1
  • 5
  • How are you linking your JS to your HTML? – CodeGodie Mar 17 '17 at 21:31
  • I have used ` ` – Hasan Rony Mar 17 '17 at 21:34
  • and where in your HTML is that located? – CodeGodie Mar 17 '17 at 21:35
  • It's located at the and of the all codes, I mean before closing the

    tag.

    – Hasan Rony Mar 17 '17 at 21:39
  • good, it should be there. Do me a favor, after `success: function(){..}` add `,error: function (data, textStatus, jqXHR) { alert(textStatus); }` Let me know what you get. – CodeGodie Mar 17 '17 at 21:42
  • Also, why are you suppressing errors on the `@mail()` function on PHP. remove the `@` . The mail function will return true or false, which you need for your conditional. – CodeGodie Mar 17 '17 at 21:47
  • I don't understand this, could you please do this and post the code here? Thank you – Hasan Rony Mar 17 '17 at 21:55
  • I cannot. Not until I have a stable answer. Look at this SO answer. Look at their JS `$.ajax` call, inside you can see the `error: function()` you need to add that to your code to catch errors. http://stackoverflow.com/questions/8918248/ajax-success-and-error-function-failure – CodeGodie Mar 17 '17 at 21:59
  • I did how you said, and when I click on submit it appears a dialogue box called "error" here is the screenshot > http://prntscr.com/elaaqu – Hasan Rony Mar 17 '17 at 22:07
  • ok alert the last parameter too, like this: `,error: function (data, textStatus, jqXHR) { alert(jqXHR); }` What do you see then? – CodeGodie Mar 17 '17 at 22:37
  • It's showing "Not Found". See this > http://prntscr.com/elaq5u – Hasan Rony Mar 17 '17 at 22:45

1 Answers1

0

It may have nothing to do with Ajax but the php native mail function; I recommend use something like phpMailer since they are much more reliable than native function.

Prav
  • 2,785
  • 1
  • 21
  • 30