简体   繁体   English

Javascript / Ajax / PHP表单验证错误

[英]Javascript/Ajax/PHP Form Validation Error

I have a form which is for checking whether or not an author is on my database (bookshop website) before allowing a new one to be entered. 我有一张表格,用于在允许输入新作者之前检查作者是否在我的数据库(书店网站)上。

This part works perfectly and authors are found and alerted to the user but when an entry is okay to submit it will not submit... 这部分工作正常,作者被发现并提醒用户,但是可以提交的条目将不会提交...

It merely does nothing. 它只是什么都不做。 The code below is the checker and then below that is the submit function which is not working... Basically if validate author is fine then it should submit but at the moment it is not. 下面的代码是检查器,然后下面的代码是不起作用的提交功能...基本上,如果验证作者是好的,那么它应该提交,但现在不行。 I have used a similar method for another form on the site and it works but this page has multiple forms on it so it may be an issue? 我对网站上的另一种表单使用了类似的方法,并且可以使用,但是此页面上有多种表单,因此可能是个问题?

var author = $("#author");
var authortext = $("#authortext");          
var authorForm = $("#addauthor");

//Author Validation
        function validateAuthor()
        {
            author.css('border', '');
            if (author.val().length == 0)
            {
                author.css('border', '2px solid red');
                authortext.text("Please enter an author.");
                return false;
            }
            else
            {
                $.ajax({
                   type: "POST",
                   url: "authorcheck.php",
                   data: 'author='+ author.val(),

                   //Stops the query being cached.
                   cache: false,
                   //The function when query accepted. 
                   success: function(result)
                   {
                        if (result == 1)
                        {
                            author.css('border', '2px solid red');
                            authortext.text("Author has been added already.");
                            return false;
                        }
                        else
                        {
                            authortext.text("Correct entry.");
                            return true;
                        }
                    }
                });
            }



        //Forms are submitted if no errors or blank values are found. The server side checks are still done later if the user has javascript disabled. 
        authorForm.submit(function()
        {
            if (validateAuthor())
            {
                return true;
            }
            else
            {
                return false;
            }
        });

use data: { author: author.val(); } 使用data: { author: author.val(); } data: { author: author.val(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM