RE: checkboxes vs text filed array and hidden variables
| From: | Tim Ward | Date: | Tue, 15 Jan 2002 09:34:05 +0000 |
| Subject: | RE: checkboxes vs text filed array and hidden variables | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-80532@lists.php.net to get a copy of this message | ||
Sounds to me like your problem is that only the checkboxes that are checked
are passed back.
e.g.
for ($I = 1; $I <= 10; $I++)
{ echo("<tr>");
echo("<td><input type='text' name='textfield[]'
value=''></td>");
echo("<td><input type='checkbox' name='cbox[]'
value='$i'></td>");
echo("</tr>");
}
when the form is submitted 2 arrays are posted, textfield and cbox.
Textfield has 10 elements but cbox has only as many elements as are ticked.
As you haven't defined keys both arrays are started at 0 in the processing
script.
My preferred solution is to define keys in the html
i.e.
for ($I = 1; $I <= 10; $I++)
{ echo("<tr>");
echo("<td><input type='text' name='textfield[$i]'
value=''></td>");
echo("<td><input type='checkbox' name='cbox[$i]'
value='1'></td>");
echo("</tr>");
}
that way in the processing script you can do
foreach (cbox as $key=>$checked) echo($textfield[$key]);
I hope I've interpreted your problem correctly.
Tim
www.chessish.com <http://www.chessish.com>
----------
From: m. ali [SMTP:mali_de@yahoo.com]
Sent: 13 January 2002 22:04
To: php-general@lists.php.net
Subject: checkboxes vs text filed array and hidden variables
Hi
my problem :
i have to pass the price, and part number which i get from the
database to
the shopping cart so when i select some
of the items by checking the checkboxes and submit the form i get
only the
last item in the list
i use hidden variables to pass these values to shopping cart.
my code look like this:
echo "<TD><input type=\"checkbox\" name=\"product[]\"
value=\"$product_id\"></TD>";
echo "<TD><input type=\"text\" size=\"2\"
name=\"quantity[$part_number]\"
value=\"1\" >";
echo"</TD>";
echo "<input type=\"hidden\" value=\"$part_number\"
name=\"part_number\"/>";
echo "<input type=\"hidden\" value=\"$discount_price\"
name=\"discount_price\"/>";