简体   繁体   English

来自AJAX请求的CodeIgniter中的表单验证失败

[英]Form validation in CodeIgniter from AJAX request is failing

An error occurs with my PHP and jQuery code as follows: 我的PHP和jQuery代码发生错误,如下所示:

jQuery: jQuery的:

$('#form').submit(function(e){
    e.preventDefault();
    var me = $(this);
    var data = me.serialize();
    $.ajax({
        method :'POST',
        url:me.attr('action'),
        data:{data},
        dataType:'json',
        async:false,
        success:function(data){
            alert(data.success);
            if(data.success == true){
                alert('true');
            }
            else{
                alert('false');
            }
        }
    });
});

HTML form: HTML形式:

    <?= form_open('practice/check', array('id'=>'form'))?>
    <input type="text" name="username">
    <input type="password" name="password">
    <button type="submit" name="button" id="submit">click</button>
    <?= form_close();?>

PHP controller: PHP控制器:

public function check(){
    $this->load->library('form_validation');
    $data= array('success'=>false, 'message'=>array());
    $this->form_validation->set_rules('username','Username','trim|required');
    $this->form_validation->set_rules('password','Password','required');

    if($this->form_validation->run() == false){
      foreach ($_POST as $key => $value) {
        $data['message'][$key] = form_error($key);
      }
    }
    else{
      $data['success'] = true;
    }
    echo json_encode($data);
}

when alert event happens in jquery code always false return even fields are filled.. please help me and thanks in advance.. 当警报事件发生在jquery代码中时,总是虚假返回,即使字段被填充..请帮助我,并提前致谢

You need to change 你需要改变

data: { data }

to

data: data

in your AJAX code. 在您的AJAX代码中。

What your version is doing is wrapping the data inside an extra object. 您的版本正在将数据包装在一个额外的对象中。 This results in a single variable called "data" being submitted to the server in the request body, like this: 这导致一个名为“数据”的变量被提交到请求主体中的服务器,如下所示:

data=username%3Duser123%26password%3Dabcdef

When what you actually want is this: 当您真正想要的是:

username=user123&password=abcdef

Therefore, PHP does not recognise the values you're sending it. 因此,PHP无法识别您发送的值。 It can only see a single variable called "data" which, as far as it's concerned, contains a random string. 它只能看到一个名为“ data”的变量,就其而言,它包含一个随机字符串。 It needs to be able to see the "username" and "password" variables separately. 它需要能够分别看到“用户名”和“密码”变量。

If you look at this JSFiddle http://jsfiddle.net/jcx2Ly5e/3/ with your Network tools open - submit the form and watch for the two requests to "test.php". 如果您在打开网络工具的情况下查看此JSFiddle http://jsfiddle.net/jcx2Ly5e/3/ ,请提交表单,并注意对“ test.php”的两个请求。 The first is incorrect and the second is correct. 第一个是不正确的,第二个是正确的。 Look inside the request body of each one to see the difference. 查看每个请求的正文以了解它们之间的区别。

PS As you'll notice, none of this has anything to do with JSON. PS正如您会注意到的那样,这些都与JSON无关。 Your PHP returns JSON, but the form data is submitted in standard form-url-encoded format. 您的PHP返回JSON,但是表单数据以标准的表单URL编码格式提交。 The JSON is not part of the issue so I edited your question to remove that. JSON不是问题的一部分,因此我编辑了您的问题以将其删除。

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

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