I have two forms. In form 1 the user checks checkboxes and in the second form. A PHP script checks the state of these checkboxes. The code is something like this:
Form 1:
----------
<input type="checkbox" name="product[][0]" value="">
<input type="text" name="product[][1]" value="">
<inpu type="text" name="product[][2]" value="">
Form 2:
----------
foreach($product as $value) {
if ($value[0] == "on") {
print "$value[1]\n";
print "$value[2]\n";
}
}
The values do get submitted to the form2 and I'm able to print out
$value[0]. It prints out as "on", but the if statement fails. If tried
trim($value[0]) it still doesn't work.
If anybody can give me help with this I will be extremely grateful.
Thanks
Tony
Checking the state of Checkboxes in PHP
Moderator: General Moderators
Try using this....
Are you sure $value[1] and $value[2] have strings in them.?
Code: Select all
<?php
foreach($_POSTї"product"] as $value) {
if ($valueї0] == "on") {
print $valueї1]."\n";
print $valueї2]."\n";
}
}
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Have you tried this in the code after submitting the form:
if you do it you'll find that your array contains the following elements:
So you need to adjust the way in which you are naming the form elements:
Then the if statement within the foreach loop will actually work. Also because the $value[0] element will only be set if the checkbox is checked change the loop to something like:
and you won't get undefined offset errors occuring.
(Change $_POST to $HTTP_POST_VARS if you are using PHP 4.0.6 or below.)
Mac
Code: Select all
echo '<pre>';
print_r($_POSTї'product']);
echo '</pre>';Code: Select all
$_POSTї'product']ї0]ї0] = 'on'
$_POSTї'product']ї1]ї1] = ''
$_POSTї'product']ї2]ї2] = ''Code: Select all
<input type="checkbox" name="productї0]ї0]" value="on">
<input type="text" name="productї0]ї1]" value="">
<input type="text" name="productї0]ї2]" value="">Code: Select all
foreach($_POSTї'product'] as $value) {
if (isset($valueї0])) {
echo $valueї1].'<br />';
echo $valueї2].'<br />';
}
}(Change $_POST to $HTTP_POST_VARS if you are using PHP 4.0.6 or below.)
Mac