deGee
deGee

Reputation: 801

codeigniter checkbox array handling issue

I have form fields which are checkboxes as below :

<input id="[email protected]" type="checkbox" checked="checked" value="[email protected]" style="float:left;border:2px dotted #00f" name="email[]">
<input id="[email protected]" type="checkbox" checked="checked" value="[email protected]" style="float:left;border:2px dotted #00f" name="email[]">
<input id="[email protected]" type="checkbox" checked="checked" value="[email protected]" style="float:left;border:2px dotted #00f" name="email[]">

But when in the controller I am taking a var_dump($this->input->post('email')) , it displays bool(false) .

in the controller I have this method :

public function referral_email()
{
    $data = $this->input->post('email');
    var_dump($data);exit;
 }

How to access this array of checkboxes in my controller ?

Upvotes: 0

Views: 1672

Answers (3)

sourcejedi
sourcejedi

Reputation: 3271

trim() works on strings, but your $_POST['email'] is an array. The correct fix should be to pass the field name as 'email[]' when using the form validation library (in the code you didn't show us but commented on later).

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields

Upvotes: 0

Talha A.
Talha A.

Reputation: 115

Are you by any chance trying to trim the output using the form validation library? I had the same problem, and removing 'trim' from the validation rules solved it.

Upvotes: 1

michaeljdennis
michaeljdennis

Reputation: 612

Make sure the form tag surrounding the text boxes has the attribute method="post" or else it might be submitting to the $_GET array. I got it to work with the following code:

Controller

public function referral_email()
{
    $data = $this->input->post('email');
    var_dump($data);
}

View

<form method="post" action="welcome/referral_email">
<input id="[email protected]" type="checkbox" checked="checked" value="[email protected]" style="float:left;border:2px dotted #00f" name="email[]">
<input id="[email protected]" type="checkbox" checked="checked" value="[email protected]" style="float:left;border:2px dotted #00f" name="email[]">
<input id="[email protected]" type="checkbox" checked="checked" value="[email protected]" style="float:left;border:2px dotted #00f" name="email[]">
<input type="submit" value="Submit" />
</form>

Upvotes: 0

Related Questions