Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在验证回调函数中访问数组字段名值?_Php_Validation_Codeigniter_Frameworks - Fatal编程技术网

Php 如何在验证回调函数中访问数组字段名值?

Php 如何在验证回调函数中访问数组字段名值?,php,validation,codeigniter,frameworks,Php,Validation,Codeigniter,Frameworks,我的视图中有一些文本输入: <label for="car-n">Car Name:</label><input type="text" name="cars[]" id="car-n"/> <label for="car-t">Car Type:</label><input type="text" name="cars[]" id="car-t"/> 那么,我应该如何访问回调函数中的这两个字段值呢?我还没有在CodeIg

我的视图中有一些文本输入:

<label for="car-n">Car Name:</label><input type="text" name="cars[]" id="car-n"/>
<label for="car-t">Car Type:</label><input type="text" name="cars[]" id="car-t"/>

那么,我应该如何访问回调函数中的这两个字段值呢?我还没有在CodeIgniters用户指南中看到有关此案例的任何信息。

好的,请查看下面的控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Temp extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library(array('form_validation'));
    }

    public function index()
    {
        $this->show_form();
    }



    public function validate(){
        $this->form_validation->set_rules('cars[]', 'Cars', 'required|callback_validate_cars');
        if ($this->form_validation->run() == FALSE)
        {
            $this->show_form();

        }
        else "All is ok";
    }

    public function validate_cars($string)
    {
        print_r($this->input->post('cars'));
        return false;
    }

    public function show_form()
    {
        echo '<form action="'. base_url('temp/validate').'" method="post">';
        echo 'Car 1: <input name="cars[]" type="text">';
        echo ' Car 2: <input name="cars[]" type="text">';
        echo '<input type="submit" value="Go!">';
        echo '</form';
    }
}

我没有使用codeigniter,但您尝试过
$this->form_validation->set_rules('cars','cars','required | xss_clean | callback_validate_cars')?请注意,
[]
您可以使用
$this->input->post('cars')
将post数据访问到您的函数中。我知道这可能很愚蠢,但您在函数名中有一个双精度的
\uu
,应该是
回调\u验证\u cars
而不是
回调\u验证\u cars
。如果你结束了这个案子,就别理我:)@manix你是对的,但出于某种原因。。。
print\r($this->input->post('cars'))
的输出是两个相同的数组(而不是一个):
Array([0]=>1[1]=>test)Array([0]=>1[1]=>test)
这没有意义,您可以发布所有控制器吗?(至少验证程序功能)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Temp extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library(array('form_validation'));
    }

    public function index()
    {
        $this->show_form();
    }



    public function validate(){
        $this->form_validation->set_rules('cars[]', 'Cars', 'required|callback_validate_cars');
        if ($this->form_validation->run() == FALSE)
        {
            $this->show_form();

        }
        else "All is ok";
    }

    public function validate_cars($string)
    {
        print_r($this->input->post('cars'));
        return false;
    }

    public function show_form()
    {
        echo '<form action="'. base_url('temp/validate').'" method="post">';
        echo 'Car 1: <input name="cars[]" type="text">';
        echo ' Car 2: <input name="cars[]" type="text">';
        echo '<input type="submit" value="Go!">';
        echo '</form';
    }
}