简体   繁体   English

PHP数组始终使用Codeigniter Framework返回false

[英]PHP array always return false with Codeigniter Framework

I have this code in my Authentication.php (Controller) 我的Authentication.php(控制器)中有此代码

public function check_user_name(){
    $userName = $this->input->post('UserName');

    $people = array();
    $people[] = array('UserName' => 'Junjun');
    $people[] = array('UserName' => 'Kent');


    if (in_array($userName, $people))
      {
        $result = array('result' => true);
      }
    else
      {
        $result = array('result' => false);
      }

    echo json_encode($result);
}

and i got my ajax code 我得到了我的ajax代码

 $(document).ready(function() {

/*/
 * Declarations
/*/
var host = 'http://' + window.location.host + '<?php echo $this->config->item('base_folder'); ?>';
var txtUserName = $('#text-user-name');
var txtPassword = $('#text-password');
var btnNext = $('#button-next');
var btnLogin = $('#button-login');
var grpUserName = $('#group-username');
var grpPassword = $('#group-password');

/*/
 * Bindings
/*/
btnNext.on('click', btnNext_Click);


/*/
 * Events
/*/ 
function btnNext_Click() {
  event.preventDefault();
  //grpUserName.addClass('hidden');
  grpUserName.find('h1').text('Checking....');

  var post_data = new FormData();

  post_data.append('UserName', txtUserName.val());

  $.ajax({
    url: 'http://localhost/pms/authentication/check_user_name',
    type: 'POST',
    dataType: 'json',
    contentType: false,
    processData: false,
    data: post_data, //post_data,
    success: function(data) { console.log(data);
      if(data.result == true){
        grpPassword.removeClass('hidden');
        grpUserName.addClass('hidden');
      }
      else{
        grpUserName.find('h1').text('Invalid User Name!');        
      }
    },
    error: function(xhr, status, error) {
      console.log(error);
    }
  });    
}
});

the problem in the console is it always returns false, what is wrong with my associative array? 控制台中的问题是它总是返回false,我的关联数组出了什么问题? help please! 请帮助!

This bit is wrong 这是错误的

$people = array();
$people[] = array('UserName' => 'Junjun');
$people[] = array('UserName' => 'Kent');

It will give you an array like this ( multi dimension ) 它会给你这样的数组(多维)

$people = [
    ['UserName' => 'Junjun'],
    ['UserName' => 'Kent']
];

so in_array fails to find the key you are looking for. 因此in_array无法找到您要查找的密钥。

 if (in_array("Junjun",[
    0 => ['UserName' => 'Junjun'],
    1 => ['UserName' => 'Kent']
])){ ... }

See "Junjun" is not equal to either of those 2 sub-arrays. 请参阅“ Junjun”不等于这两个子数组中的任何一个。 Instead just add them: 相反,只需添加它们:

   $people = array();
   $people[] = 'Junjun';
   $people[] = 'UserName';

   //or
   $people = array('Junjun','UserName');

So your array looks like this 所以你的数组看起来像这样

   $people = ['Junjun','Kent'];

Problem : you are checking in_array condition with multi array 问题 :您正在使用多数组检查in_array条件

Solution : You should change your people array something like below and it will work. 解决方案 :您应该更改人员数组,如下所示,它将起作用。

    public function check_user_name(){
        $userName = $this->input->post('UserName');

        $people[] = 'Junjun';
        $people[] = 'Kent';


    if (in_array($userName, $people))
      {
        $result = array('result' => true);
      }
    else
      {
        $result = array('result' => false);
      }

    echo json_encode($result);
}

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

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