无法执行<form>标记中指定的操作

时间:2015-12-10 06:04:17

标签: php jquery html5

我使用jquery来执行一个php文件,但问题是在执行php文件之后它没有执行<form>标签中指定的“action”...请帮我解决这个问题!请原谅我错了。

这里有 .js 文件

$(document).ready(function(){

    //execute's the function on click
    $("#submit").click(function(e){

        /*jquery to call the url requested 
        and parse the data in json*/
        $.ajax({
            url: "register.php",
            type: "POST",
            data: {
                fname: $("#fname").val(),
                lname: $("#lname").val(),
                email: $("#email").val(),
                password: $("#password").val(),
                mobile: $("#mobile").val()
            },
            dataType: "JSON",
            /*Give out the alert box
            to display the results*/ 
            success: function (json){
                if(json.error){
                    alert(json.error_msg);
                    e.preventDefault();
                }else{
                    alert("Registeration successful!",json.user.email);
                }
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert(errorThrown);
                e.preventDefault();
            }
        });
    });

   }); 

这里有 .html 文件

    <html>
        <head>
    <title>jQuery Test</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script type="text/javascript" src = "register.js"></script>
     </head>

     <body>

        <!--html body-->
        <form name = "register" action = "login.html" id = "register" method = "POST">
            <label>First name:</label>
            <input type = text name = "fname" id = "fname" required>
            <label>Last name:</label>
            <input type = "text" name = "lname" id = "lname" required>
            <label>E-mail:</label>
            <input type = "email" name = "email" id = "email" required>
            <label>Password</label>
            <input type = "password" name = "password" id = "password" required>
            <label>Mobile no:</label>
            <input type = "number" name = "mobile" id = "mobile" required>
            <input type="submit" value="Insert" name="submit" id = "submit">
        </form>
        <div id = "result" align = "right"></div>
    </body>
    </html>  

1 个答案:

答案 0 :(得分:2)

删除此行

e.preventDefault();

如果你想防止错误提交,请在ajax的错误部分添加该行,

error: function(jqXHR, textStatus, errorThrown){
     alert(errorThrown);
     e.preventDefault();//<--- add here so that on error, your form is not submitted.
}