【问题标题】:PHP array always return false with Codeigniter Framework使用 Codeigniter 框架时 PHP 数组总是返回 false
【发布时间】:2017-01-04 04:46:44
【问题描述】:

我的 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);
}

我得到了我的 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);
    }
  });    
}
});

控制台中的问题是它总是返回 false,我的关联数组有什么问题?请帮忙!

【问题讨论】:

  • 尝试var_export( $_POST ) 看看它的真实内容是什么。 CI有一个小写的坏习惯。所有错误的意思是,该项目不存在于$_POST
  • 您的人员数组结构错误。
  • @ArtisticPhoenix 先生,我应该把那个放在哪里?我应该在 $_POST 中放入什么 post 变量?
  • @ArtisticPhoenix 我也认为我的结构是错误的,但我不想使用 $people = array('junjun', 'kent');东西
  • @OJT - 添加了一个答案,应该能说明问题。

标签: php arrays ajax codeigniter


【解决方案1】:

这一点是错误的

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

它会给你一个这样的数组(多维)

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

所以in_array 找不到您要查找的密钥。

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

见“君君”不等于这两个子数组中的任何一个。相反,只需添加它们:

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

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

所以你的数组看起来像这样

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

【讨论】:

  • 成功了!谢谢伙计,我也想到了我的结构,该死,没想到会发生这种情况,谢谢你让我明白,新年快乐
  • 你也是,很高兴我能帮上忙。当我遇到无法解决的问题时,我会继续前进……哈哈……帮助我思考如何帮助其他人。
  • 解释+1。
【解决方案2】:

问题:您正在使用多数组检查 in_array 条件

解决方案:您应该像下面这样更改人员数组,它会起作用。

    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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2017-07-11
    • 2020-12-27
    相关资源
    最近更新 更多