CodeIgniter Forums
Capturing multiple select form input - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Capturing multiple select form input (/showthread.php?tid=2222)



Capturing multiple select form input - El Forum - 07-23-2007

[eluser]millo[/eluser]
Please help.

I've got a form that looks like this:

Code:
<form action="http://test.dev/admin/document_assign_update" method="post">
<input type="hidden" name="document_id" value="1" />
<select type="text" name="username[]" multiple="multiple" size="4">
   <option value="user1"><br />classname1</option>
   <option value="user2"><br />classname2</option>
   <option value="user3"><br />classname3</option>
</select>
&lt;input type="submit" value="submit" /&gt;
&lt;/form&gt;

How do I capture the data from the multiple select?

I've put something as simple as this in the controller just to see what it outputs (not using any views at the moment):

Code:
function document_assign_update() {
print_r($_POST);
}

When I don't select anything from the drop down and submit I get this output ...

Code:
Array ( [document_id] => 1 )

... which is fine, but then if I select an option from the drop down and then submit I get an error message related to the database:

Code:
An Error Was Encountered

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT username, role FROM auth WHERE username = Array AND password = 'd41d8cd98f00b204e9800998ecf8427e'

why am I getting this error when I'm quite simply asking for the post output with print_r($_POST); ???

Can someone see what I'm doing wrong?


Capturing multiple select form input - El Forum - 07-23-2007

[eluser]esra[/eluser]
Do a forum search for 'multi select'. You will probably get a lot of hits.


Capturing multiple select form input - El Forum - 07-23-2007

[eluser]millo[/eluser]
I've searched for multiple select and hence found the way of using print_r($_POST); but I haven't found anyone who's had the same problem as me (yet). Is this a common problem?


Capturing multiple select form input - El Forum - 02-21-2008

[eluser]zeroeighteen[/eluser]
[quote author="millo" date="1185248583"]

Code:
An Error Was Encountered

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT username, role FROM auth WHERE username = Array AND password = 'd41d8cd98f00b204e9800998ecf8427e'

why am I getting this error when I'm quite simply asking for the post output with print_r($_POST); ???
[/quote]

a) looks like your code is moving on AFTER doing the print_r($_POST) with a database call. try putting exit(); after print_r($_POST);
b) looks like your database call is getting the Array. try a foreach on the Array and doing those calls one at a time.


Capturing multiple select form input - El Forum - 02-11-2010

[eluser]johnpeace[/eluser]
If you want the select to be a multi-select, it's going to need [] appended to it's name and will return an array like array(0 => 'value 1', 1 => 'value 2' ...)

But that's not what it looks like you want.

If your query reads:
SELECT username, role FROM auth WHERE username = Array AND password = '...

It looks like your code is expecting a scalar value there, username would only ever = 1 value.

So, just remove the 'multiple' and the [] from your select tag, which will turn it into a regular dropdown from which one value can be submitted.

If you WANT an array of values from that field, you'll need to do something with the array to turn it into a where clause you can work with, like:

// set starts with a comma? nope!
$values = '0';
// loop through the array coming from the multiselect
foreach ($this->input->post('username') as $key => $value) {
// append whatever values are found to the variable
$values .= ', ' . $value;
}

Then your query can use:
SELECT whatever FROM table WHERE value IN($values)

...or something like that.


Capturing multiple select form input - El Forum - 02-11-2010

[eluser]danmontgomery[/eluser]
the "username" multiselect is probably being picked up by a login script. I would just change the field name to something other than username.