【问题标题】:Value of Multiple select input text field in CodeIgniter controllerCodeIgniter 控制器中多选输入文本字段的值
【发布时间】:2017-11-28 16:04:05
【问题描述】:

如何在 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;

你有什么建议吗?

【问题讨论】:

标签: php html codeigniter


【解决方案1】:

您的值将发布在一个数组中:

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

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

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

【讨论】:

  • 我试过这个方法。我没明白。所以我发布了这个问题。
  • 诀窍是,您可以像在代码示例中一样使用名称属性 student[],并在检索它时在代码中使用它而不使用括号,如 Kisaragi 所示。
【解决方案2】:

在回显帖子值时无需使用 [],您已在 name='student[]' 中定义;

 $studentname =$this->input->post('student');
    echo "<pre>";
    print_r($studentname);
    echo "</pre>";

【讨论】:

    【解决方案3】:

    为了命名,让它成为学生,因为它是多选的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      相关资源
      最近更新 更多