0

我有这个表单提交代码:

索引.php

    <html>
    <head>
        <title>My Form</title>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

        <script type="text/javascript">

            $(document).ready(function(){

                $("#myForm").submit(function(){
                    var tvyalner = $("#myForm").serialize();
                    $.ajax({
                        type: "POST",
                        url: "postForm.ajax.php",
                        data: tvyalner,
                        dataType: "json",

                        success: function(data){
                            $("#formResponse").removeClass('error');
                            $("#formResponse").addClass(data.status);
                            $("#formResponse").html(data.message);
                            if(data.status === 'success'){$("#myForm table").hide();}
                        },
                        error: function(){
                    $("#formResponse").removeClass('success');
                    $("#formResponse").addClass('error');
                    $("#formResponse").html("There was an error submitting the form. Please try again.");
                        }
                    });

                    //make sure the form doens't post
                    return false;


                });


            });
        </script>

        <style type="text/css">

            .success{

                border: 2px solid #009400;
                background: #B3FFB3;
                color: #555;
                font-weight: bold;

            }

            .error{

                border: 2px solid #DE001A;
                background: #FFA8B3;
                color: #000;
                font-weight: bold;
            }
        </style>

    </head>
    <body>
<div class="mfm">
    <form id="myForm" name="myForm" method="post" action="" style="margin: 0 auto; width: 300px;">

        <div id="formResponse"></div>

        <table>

            <tr><td>Name:</td><td><input name="name" type="text" value=""></td></tr>
            <tr><td>Email:</td><td><input name="email" type="text" value=""></td></tr>
            <tr><td>Message:</td><td><textarea name="message" rows="5" cols="20"></textarea></td></tr>
            <tr><td>&nbsp;</td><td><input type="submit" name="submitForm" value="Submit Form"></td></tr>

        </table>

    </form>
</div>  
    </body>
</html>

postForm.ajax.php

    <?php

//function to validate the email address
//returns false if email is invalid
function checkEmail($email){

    if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)){
        return FALSE;
    }

    list($Username, $Domain) = split("@",$email);

    if(@getmxrr($Domain, $MXHost)){
        return TRUE;

    } else {
        if(@fsockopen($Domain, 25, $errno, $errstr, 30)){
            return TRUE; 
        } else {

            return FALSE; 
        }
    }
}   



//response array with status code and message
$response_array = array();

//validate the post form

//check the name field
if(empty($_POST['name'])){

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'Name is blank';

//check the email field
} elseif(!checkEmail($_POST['email'])) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'Email is blank or invalid';

//check the message field
} elseif(empty($_POST['message'])) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'Message is blank';


//form validated. send email
} else {

    //send the email
    $body = $_POST['name'] . " sent you a message\n";
    $body .= "Details:\n\n" . $_POST['message'];
    mail($_POST['email'], "SUBJECT LINE", $body);

    //set the response
    $response_array['status'] = 'success';
    $response_array['message'] = 'Email sent!';

}


echo json_encode($response_array);

?>

首次提交后显示:

名称为空。

填写字段并再次提交。显示

有一个错误提交表单。请再试一次。

就这样。

代码在本地主机中不起作用。

问题是什么?

4

1 回答 1

1

我建议使用这个错误函数来查看错误是什么:

$.ajax({
    /* ... */
    error: function(jqXHR, textStatus, errorThrown){

        console.log(errorThrown); // Or use alert()
        console.log(textStatus);

        $("#formResponse").removeClass('success');
        $("#formResponse").addClass('error');
        $("#formResponse").html("There was an error submitting the form. Please try again.");
    }
});
于 2013-01-10T09:00:37.943 回答