【问题标题】:Jquery Remote Validation Rule - php email check failingJquery 远程验证规则 - php 电子邮件检查失败
【发布时间】:2013-07-23 10:10:39
【问题描述】:

我已经扫描了很多天的互联网,并尝试了几乎所有发布的内容来解决问题。我想要做的是(像许多其他帖子一样),通过远程验证发送远程 mysql 查询。关于返回数据的正确格式(如 json_encode 与否)存在很多争论,我已经尝试了这两个建议没用。

jQuery 代码

    $('#register-form-step-1').validate({  // initialize plugin
    rules:
    {
        confirmEmail:
        {
            equalTo: "#clientEmailAddress"
        },
        clientPassword:
        {
            rangelength: [6,32],
            required: true
        },
        clientUserName:
        {
            minlength: 4,
            required: true,
            remote:
            {
                async:false,
                type:'POST',
                url:'<?php echo base_url("home/checkusername")?>',
                data: {
                clientUserName: function() {
                return $("#clientUserName").val();
                }},
                success: function(data)
                {
                    console.log(data);
                    if (String(data) === String('true'))
                    {
                        //not registered
                        console.log("Not registered");
                        return true;
                    }
                    else
                    {
                    console.log(data);
                        //already registered
                        console.log("Already registered");
                    }
                },
                error: function()
                {
                    console.log("There was an error");
                }
            }
        },
        clientEmailAddress:
        {
            async:false,
            required: true,
            email: true,
            remote:
            {
                type:'POST',
                url:'<?php echo base_url("home/checkemail")?>',
                data: {
                clientEmailAddress: function() {
                return $("#clientEmailAddress").val();
                }},
                success: function(data)
                {
                    console.log(data);
                    if (String(data) === String('true'))
                    {
                        //not registered
                        console.log("Not registered");
                        return true;
                    }
                    else
                    {
                        //already registered
                        console.log("already registered");
                    }
                },
                error: function()
                {
                    console.log("There was an error");
                }
            }       
        }
    },
    submitHandler: function ()
    {
        $.ajax({
            type: 'POST',
            url: '<?php echo base_url("home/register")?>',
            data: $('#register-form-step-1').serialize(),
            success: function ()
            {
                alert('success')
                console.log('form was submitted');
                $("#register-form-1").modal('hide');
                $("#register-form-2").modal('show');
            },
            error: function(data, textStatus, jqXHR) {
                  alert('error')
            }
        });

        return false; // ajax used, block the normal submit
    }
});

PHP 代码

    public function checkemail()
{

    $email = mysql_real_escape_string($_POST['clientEmailAddress']);

    $qResult = $this->db->query('
    SELECT clientEmailAddress FROM clientdata WHERE clientEmailAddress = "'.$email.'" limit 1
    ');

    $result = true;

    if ($qResult->num_rows == 1)
    {
        $result = false;
    }

    header('Content-Type: application/json');
    echo json_encode($result);

}

【问题讨论】:

    标签: php jquery json validation jquery-plugins


    【解决方案1】:

    替换php中的行

    echo json_encode($result);
    

    通过

    echo json_encode(array($result));
    

    并在js 中添加datatypejson

    否则你可以试试

    echo 1 or 0; return;
    

    【讨论】:

    • 感谢您的快速回复。 .我按照您的建议调整了代码,表单仍然没有验证.. 没有像往常一样返回错误.. 控制台现在显示 [true] 或 [false] 但不验证表单
    • 你把它括在square brackets 中意味着它是一个array。对吗???如果是,那么cast它。
    • 对.. 一个数组,我不确定回复应该有多“精确” :) 我将尝试转换返回数据。
    • ok ...所以返回数据是一个数组..在js脚本中使用jsonParse应该去掉数据..我卡住的部分......json_encode数据是什么“验证“ 表格。那么为什么要在“经过”验证/未验证之后在另一端投射数组呢
    • 仍然有这个问题..正在对从 json_encode 到 jquery 处理程序的远程和 php 回显数据做更多的工作......当使用任何形式的 parseJSON 或只是试图返回时,我不断得到 UNDEFINED对象...这可能是问题的一部分,因为返回的 json 数据为空或被解释为无效?
    【解决方案2】:

    行前:

    header('Content-Type: application/json');
    

    添加:

    header("HTTP/1.1 200 OK");
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,但最终示例函数为我解决了问题。

      function checkIfEmailExists($email){
          $query="SELECT email FROM users WHERE email='".$email."'";
          $link=@mysql_query($query);
          $count=mysql_num_rows($link);
      
          $response='';
          if($count==1){
              $response=false;
          }else if($count==0){
              $response=true;
          }
          header('Content-Type: application/json');
          echo json_encode($response);
          return;
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-02
        • 2015-06-15
        • 1970-01-01
        • 2018-04-02
        • 2018-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多