Ajax联系表单在网站上不起作用

时间:2017-03-17 21:23:43

标签: javascript php jquery html ajax

我只是一个初学者并尝试使用php邮件脚本实现ajax联系表单。但是,当我点击“提交”时,没有任何反应,也没有任何内容。

以下是我的代码。

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

有人可以修改此代码吗?我不知道它有什么问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

它可能与Ajax无关,但与php本机邮件功能无关;我建议使用像phpMailer这样的东西,因为它们比原生函数更可靠。