0

I've got an HTML form which allows a user to select multiple options in a dropdown. I then pass that data on as post data to a PHP backend (I'm using codeigniter for the backend, and the data is being passed to a model).

In javascript, I can log the value being passed, and if there are multiple values, it shows as a proper, comma separated string of values. But if log the value in PHP, it only lists the last of the multiple values. How do I get it to retain all selections? This is my code:

FORM:

<form action="http://localhost:8888/index.php/control_form/add_all" method="post" accept-charset="utf-8">
<label for="sel_dep">Member Dependencies</label> 
<select id="sel_dep" name="sel_dep" multiple> 
    <option value=""></option> 
    <option value="4">Soundgarden</option>
    <option value="5">Rage Against the Machine</option>
    <input type="submit" name="submit" value="Submit"  />       
</select> 
</form>

PHP Codeigniter Model:

function edit_member(){
  if (isset($_POST['submit'])) {
    $v_memberdep = $this->input->post('sel_dep');
    log_message('debug', 'DEP: ' . $this->input->post('sel_dep'));
  }
}

1 Answer 1

2

Set the name of your select box to f_memberdep[] the [] will tell PHP that it should be passed as an array so you will receive all values.

Also should point out that your logging a field called sel_dep when your select box is called f_memberdep but that's probably just a formatting thing.

Sign up to request clarification or add additional context in comments.

1 Comment

Just a note that in order to actually pass that data on to a DB field, you have to either create a completely separate table for that data to go into, or you need to serialize the data so it can go into a single field (which is what I did. Thanks fire

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.