PHP邮件脚本在ajax调用和表单提交后未完成

时间:2014-02-06 12:43:12

标签: javascript php jquery ajax forms

我试图在使用AJAX提交表单后调用php脚本。 PHP脚本被调用但除此之外没有任何事情发生,没有收到电子邮件,没有错误消息等。但是如果通过URL直接访问php文件并且success属性确实正确执行表单重置,它将重定向到主页。 TIA!

HTML:

<form id="form-box">
        <img id="form-logo" src="images/framework/logo.png">
        <p>
            <input name="first-name" id="first-name" type="text" maxlength="32" placeholder="First Name">
            <label for="first-name">First Name</label></p>
        <p>
            <input name="last-name" id="last-name" type="text" maxlength="32" placeholder="Last Name">
            <label for="last-name">Last Name</label></p>
        <p>
            <input name="email" id="email" type="text" placeholder="E-Mail">
            <label for="email">E-Mail</label></p>
        <p>
            <textarea name="comment-feedback" id="comment"></textarea></p>
        <p>
            <input type="button" id="submit" value="Submit"></p>
    </form>

JavaScript的:

    $("#submit").on('click', function(){
        var fileName = document.location.pathname.match(/[^/]+$/),
            firstName = $('#first-name').val(),
            lastName = $('#last-name').val(),
            emailVal = $('#email').val(),
            comments = $('#comment').val();

        $.ajax({
            type: 'POST',
            url: '../script/mail.php',
            data: {fname:firstName, lname:lastName, email:emailVal, comment:comments, fileName:fileName},
            success: function(){
                $('form').each(function() {
                    this.reset();
                });
            }
        });
    });

PHP:*编辑:删除验证用于测试目的,这仍然不起作用。

<?php
$webmaster_email = "feedback@myDomain.com";
$index = "http://mydomain.com";
$email_address = $_POST['email'];
$comments = $_POST['comment'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];

echo "$email_address";
echo "$comments";
echo "$fname";
echo "$lname";

/*function isInjected($str) {
    $injections = array('(\n+)',
        '(\r+)',
        '(\t+)',
        '(%0A+)',
        '(%0D+)',
        '(%08+)',
        '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
        return true;
    }
    else {
        return false;
    }
}*/

if (!isset($_REQUEST['email'])) {
    header("Location: $index");
    exit;
}


elseif (empty($email_address) || empty($comments) || empty($fname)) {
    echo "<script type=\"text/javascript\">alert('Please fill in the required fields.');</script>";
    exit;
}

// If email injection is detected, redirect to the error page.
elseif (isInjected($email_address)){
    echo "<script type=\"text/javascript\">alert('Please, Try Again.')";
    exit;
}

// If we passed all previous tests, send the email then redirect to the thank you page.
else {
    mail("$webmaster_email", "Feedback Form Results", $comments, "From: $email_address");
    echo "<script type=\"text/javascript\">alert('Thank You for contacting us!')";
    exit;
}
?>

0 个答案:

没有答案