Welcome Guest, Not a member yet? Register   Sign In
Saving checkboxes
#1

[eluser]easymind[/eluser]
Once it worked, but now I am lost. I want to save checkboxes so I have a form like:
Code:
<form method="POST" action="somehwere.php">
&lt;input type="checkbox" name="id_categories[]" value="1"&gt; option1<br/>
&lt;input type="checkbox" name="id_categories[]" value="2"&gt; option2<br/>
&lt;input type="checkbox" name="id_categories[]" value="3"&gt; option3<br/>
&lt;input type="submit"&gt;
&lt;/form&gt;
It seems I cannot read the values anymore when posted. I cannot access the data with:
Code:
$this->input->post('id_categories');
or
$_POST['id_categories']
even when I do:
echo "<pre>".print_r($_POST,1)."</pre>";
I only see the word 'array' where my checkbox array should be.
#2

[eluser]easymind[/eluser]
Ok hm. I found here http://www.phpfreakz.nl/ that it could be a server issue. If I try to get the array out of the $_REQUEST string, it works....
#3

[eluser]Negligence[/eluser]
It says array because it is an array of values. You need to do:

Code:
$_POST['id_categories'][0]
$_POST['id_categories'][1]
$_POST['id_categories'][2]
etc...

Don't use REQUEST.
#4

[eluser]Michael Wales[/eluser]
But, it's just saying Array() - not outputting the values from that array, right (that's what I gathered).
#5

[eluser]ejangi[/eluser]
I thought CI cleared the $_POST array as part of its' security measures??? So, wouldn't you then go:
Code:
echo "<pre>".print_r($this->input->post('id_categories'),1)."</pre>"
#6

[eluser]Michael Wales[/eluser]
It doesn't clear $_POST, it just santizes it (so $_POST and $this->input->post return the exact same thing).
#7

[eluser]ejangi[/eluser]
Yeah thanks Michael, immediately after posting that I went and checked the source of Input.php Tongue

The other thing I wanted to ask Negligence is: When you get the blank Array(), have you checked any of the checkboxes? Cause I'm pretty sure the browser won't send any details about that form element unless it's been checked. I'm sure you've checked this, I just thought I'd double check...

[EDIT]: That's a lot of "checks" in one sentence. Tongue
#8

[eluser]Negligence[/eluser]
If you don't click a checkbox, it actually doesn't appear in the $_POST array at all. Not even as a null/empty value.

easymind, I think the problem has to do with you doing an echo() and print_r() in the same statement. Anytime you try to echo() an array, it will print "Array". You should be doing this:

Code:
echo '<pre>';
print_r($array);
echo '</pre>';

And use a for/foreach loop to walkthrough the checkbox values.
#9

[eluser]easymind[/eluser]
People, I am sure it was a server issue. If a $_POST variable is an array my server makes it a string. I really need to use the $_REQUEST variable to get my array....

This can be helpfull information for the rest of the 0,000001% of people who have this same problem....
#10

[eluser]Negligence[/eluser]
I am telling you that you cannot print arrays using echo(), this isn't a bug. This is just the way PHP works. If you try to use echo() on an array, it will just print "Array". Use my code above and your problem will be solved.

And I advise against using $_REQUEST because you don't actually know where the data is coming from. It's bad practice.




Theme © iAndrew 2016 - Forum software by © MyBB