0

好的,所以我有这个功能

<?php

/**
 * @author Mitchell A. Murphy
 * @copyright 2011
 */
include ('func_lib.php');
connect();
echo (check($_POST['input']) ? 'true' : 'false');
function check($args)
{
    $args = strtolower($args);
    $checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
    if (preg_match($checkemail, $args))
    {
        //logic for email argument
        $sql = "SELECT * FROM `users` WHERE `email`='" . $args . "'";
        $res = mysql_query($sql) or die(mysql_error());
        echo "type=email:";
        if (mysql_num_rows($res) > 0)
        {
            return true;
        } else
        {
            return false;
        }
    } else
    {
        //logic for username argument
        $sql = "SELECT * FROM `users` WHERE `username`='" . $args . "'";
        $res = mysql_query($sql) or die(mysql_error());
        echo "type=username:";
        if (mysql_num_rows($res) > 0)
        {
            return true;
        } else
        {
            return false;
        }
    }

}

?>

此 jquery 脚本应访问该函数:

$('form.register .submit').click(validateRegister);

function validateRegister() {

    //Variables
    var emailExists = false;
    var userExists = false;
    var $error = "";

    //Executes functions
    email();


    function email() {
        var $error = $('#email .error');
        var input = $('#email input').val();
        var emailRE = /^.*@.+\..{2,5}$/;
        if (input.match(emailRE)) {
            $error
                .html('<div>Proper Email Format: <span>Hello@Yoursite.com</span></div>')
                .animate({
                'left': '-130px',
                'opacity': '0'
            });

            //Checks for Existing Email

            function checkExisting_email() {
                $.ajax({
                    type: 'POST',
                    url: 'includes/checkExist.php',
                    data: input,
                    statusCode: {
                        404: function () {
                            alert('page not found');
                        }
                    },

                    success: function (data) {
                        alert(data);
                    },
                    error: function () {
                        alert("error bro");
                    }
                });

            }
            emailExists = checkExisting_email();

            //If it exists
            if (emailExists) {
                alert("This email already exists!");
            } else if (emailExists == false) {
                alert("Email doesnt exist!");
            }

        } else {
            //Email doesn't match
            $error
                .html('<div>Proper Email Format: <span>Hello@Yoursite.com</span></div>')
                .animate({
                'left': '-150px',
                'opacity': '1'
            });
        }
    }
    return false;
}

但是由于某种原因,脚本(js)没有发送任何数据?如果是这样,我该如何引用它。我是后端开发人员,但做 javascript 的设计师让我来解决这个问题。我知道 php 可以工作,因为我制作了一个测试表单来使用这个 html 标记发送数据:

<form action="includes/checkExist.php" method="post">
<input type="text" name="input" />
<input type="submit" name="submit" />
</form>

这有效......那么为什么来自jquery的输入返回为NULL?

4

2 回答 2

1

看到checkExisting_email()不返回任何东西,所以emailExists = checkExisting_email();不会设置emailExists。此数据将仅在回调函数中提供,今天仅在alert().

为了让事情变得更简单,请使用 jQuery ajax 验证字段remote。检查文档和示例

于 2011-05-09T05:14:39.323 回答
1

您需要为“数据”传递一个键/值对,而不仅仅是值。

照原样,您的表单将使用如下所示的查询字符串发布:

target.php?asdf@hotmail.com

它应该是:

数据:{输入:输入},

这会将查询字符串设置为:

target.php?input=asdf@hotmail.com

此外,由于您是通过 ID 从元素中获取值,因此您不需要指定输入标签。

 var input = $('#email').val();
于 2011-05-09T05:44:25.797 回答