Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sizeof($_POST) is killing me slowly

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Can someone tell me how I could make the size of the $_POST array always the same whether I use checkboxes or not???

Unselecetd checkboxes don't appear in $_POST and it is breaking all my code.

I assumed that unselected checkboxes would appear as empty values in $_POST the same way textfields do but they don't.

Thanks for helping.
 
I'm not sure what you're asking.

What I do with checkboxes is to use isset(), array_key_exists(), or in_array() to see whether the name of the checkbox exists in $_POST. If it does not, my script assumes that the checkbox was not checked.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
How are you specifying the names of the check boxes in your form? If you can show us that, we might be able to suggest a different way of checking for them.

Ken
 
Thanks for caring ! :)

My problem is that I absolutely need to loop through all the values of the $_POST array BUT the size of this array MUST always equal the number of form elements independently of the fact that checkboxes can be used or not.

I hope you can understand my urgent need.
Thanks again !

Code:
(|)(|)
Code:
<=~x~>
&quot; & & &quot;
 

Ooops ... I forgot to remove my sig which melted with my post in a very weird way :)
Too bad we can't edit our posts :(
 
To be more specific, I'd like this :

<?php

if($_POST) {

print(nl2br(print_r($_POST, true)));

} else {

}

echo"
<form method=\"POST\" action=\"test.php\">

<input type=\"checkbox\" name=\"cb_1\" value=\"yes\"><br />
<input type=\"checkbox\" name=\"cb_2\" value=\"yes\"><br />
<input type=\"checkbox\" name=\"cb_3\" value=\"yes\"><br />
<input type=\"checkbox\" name=\"cb_4\" value=\"yes\"><br />
<input type=\"submit\" value=\" SUBMIT \">
</form>
";

?>

... to output this :

Array
(
[cb_1] =>
[cb_2] =>
[cb_3] =>
[cb_4] =>
)

... instead of nothing when elements aren't checked.
 
Here's one way of getting the results you're looking for.
Code:
<?php
$tmp = array();
$tmp[] = '<form method="POST" action="' . $_SERVER['PHP_SELF'] .'">';
for ($i=0;$i<4;$i++) {
        $x= '<input [COLOR=red]type="hidden"[/color] name="cb[' . $i . ']" value="no">';
        $x .= 'Test ' . $i .'<input type="checkbox" name="cb[' . $i . ']" value="yes">';
        $tmp[] = $x; }
$tmp[] = '<input type="submit" value="Send Form" name="submit">';
$tmp[] = '</form>';
echo implode("<br />\n",$tmp)."<br />\n";
if(isset($_POST['submit'])) {echo '<pre>';print_r($_POST);echo '</pre>';}
?>
It uses the "type=hidden" form field to initialize the variables to the unchecked value, then if you check any of the boxes, the corresponding checked value is returned.

I changed the names you used from individual names to an array, since arrays are easier to manipulate in PHP.

I have tested the code and it works. (PHP 4.3.2)

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top