1

How can I get the value of a multiple select input field in CodeIgniter controller?

I want a multiple select input field shown here

I have added below html code.

<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>

I am using given code to fetch the value of this input field. But I didn't get the value.

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

Do you have any suggestions?

1

3 Answers 3

2

Your values will be posted in an array:

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

You can then access each value using loop:

foreach($studentNames as $name){
    echo "Student name is $name";
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried this method. I didn't get it. So I posted this question.
The trick is, that you use the name attribute student[] like in your code example and use it woithout the brackets in your code when retrieving it, like shown by Kisaragi.
0

there is no need to use [] while you are echoing post value, you already define in name='student[]';

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

Comments

0

for naming sake make it student students since its a multislect

<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>

in controller:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.