CodeIgniter控制器中多个选择输入文本字段的值

时间:2017-11-28 16:04:06

标签: php html html5 codeigniter

如何在CodeIgniter控制器中获取多选输入字段的值?

I want a multiple select input field shown here

我已添加以下html代码。

<select class="js-example-basic-multiple  multiple_selection form-control" name="student[]" multiple="multiple"  >
                <option value="1">Student1</option>
                <option value="2">Student2</option>
                <option value="3">Student3</option>
                <option value="4">Student4</option>
                <option value="5">Student5</option>
                <option value="6">Student6</option>
              </select>

我正在使用给定的代码来获取此输入字段的值。但我没有得到这个价值。

$studentname = array();
    $studentname =$this->input->post('student[]');
    echo 'studentName:'.$studentname;

你有什么建议吗?

3 个答案:

答案 0 :(得分:2)

您的值将以数组的形式发布:

$studentNames = $this->input->post('student');

然后,您可以使用循环访问每个值:

foreach($studentNames as $name){
    echo "Student name is $name";
}

答案 1 :(得分:0)

在回显帖子值时没有必要使用[],你已经在name ='student []'中定义;

TimeSpan reded;

bool success = TimeSpan.TryParseExact(intervalString, "hh\\:mm\\:ss",
    CultureInfo.InvariantCulture, out reded);

答案 2 :(得分:0)

用于命名,因为它是多选的

,因此可以让学生成为学生
<select class="js-example-basic-multiple  multiple_selection form-control" name="students[]" multiple="multiple"  >
    <option value="1">Student1</option>
    <option value="2">Student2</option>
    <option value="3">Student3</option>
    <option value="4">Student4</option>
    <option value="5">Student5</option>
    <option value="6">Student6</option>
 </select>
控制器中的

//get the post values of students (if you dump the post you will see that $_POST['students'] is an array
$students = $this->input->post('students');

https://www.codeigniter.com/user_guide/libraries/input.html

//will contain the values of the selected students ex. var_dump($students) will produce [1,2,4];

//you will need to loop over students since it is an array

foreach($students as $id){
  echo "Student id is {$id}";
}