关于javascript:Ajax联系表单在网站上不起作用

Ajax contact form not working in website

我只是一个初学者,并试图用PHP邮件脚本实现ajax联系表单。 但是,当我点击"提交"时,没有任何反应,也没有任

以下是我的代码。

HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
// Get values from jquery
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];

$to ="[email protected]";
$subject ="New Email Subscriber";
$message =" Name:" . $name ."



 Email:"
. $email ."



 Address:"
. $address ."



 Phone:"
. $phone;


$from ="ContactForm";
$headers ="From:" . $from ."

"
;
$headers .="Content-type: text/plain; charset=UTF-8" ."

"
;

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

有人可以修复此代码吗? 我不是有什么不对的。

提前致谢。