Reputation: 47
I have an html form that passes an array into php and then does a for loop to print out values. This is the code that I have
$payloads = $_POST['topay'];
$loadNum = $_POST['loadnum'];
$unit = $_POST['unit'];
$driver = $_POST['driver'];
for($i=0;$i<count($payloads);$i++)
{
echo $payloads[$i];
echo "<br>";
echo '<td width="50" valign="top">'.$loadNum[$i].'</td>';
echo '<td width="50" valign="top">'.$unit[$i].'</td>';
echo '<td width="150" valign="top">'.$driver[$i].'</td>';
}
the $_POST['topay'] is a checkbox array that I pass in where only the items that are checked will come through. For example in the form that I pass to php there are multiple check boxes, and it should only pass the ones that are checked. When I do the first echo in the for loop it correctly prints out the values, but the echos within the do not.
Values:
$loadNum = [5,6]
$unit = [101,103]
driver = ["joe", "mike"]
When both of these checkboxes are checked, the output works correctly. When the first checkbox is checked, the output works correctly. However, if I only check the second one (6, 103, "mike"), the output still comes out for (5,101,"joe")
EDIT: here is the HTML code that i send to this php page:
echo '<form action="statement.php" target="_blank" METHOD="post">';
echo '<td><input type="checkbox" name="topay[]" value="'.$loadNumber.'" checked></td>';
echo '<td width="70"><input type="hidden" name="loadnum[]" value="'.$loadNumber.'" />'. $loadNumber.'</td>';
echo '<td width="70"><input type="hidden" name="unit[]" value="'.$unit.'" />' .$unit.'</td>';
echo '<td width="150"><input type="hidden" name="driver[]" value="'.$driver.'" />' .$driver.'</td>';
?>
<p><input type="submit" name="sumbit" value="Create Statement" /></p>
Upvotes: 0
Views: 76
Reputation: 173522
Assuming you have properly named the input values, e.g.:
<input name="topay[0]" type="checkbox">
<input name="loadnum[0]" value="5">
<input name="unit[0]" value="101">
<input name="driver[0]" value="joe">
<input name="topay[1]" type="checkbox">
<input name="loadnum[1]" value="6">
<input name="unit[1]" value="103">
<input name="driver[1]" value="mike">
Take note of the topay[0]
and topay[1]
notation that I'm using, as opposed to your form input values that use topay[]
:
<input type="checkbox" name="topay[]" value=" ...
<input type="hidden" name="loadnum[]" ...
<input type="hidden" name="unit[]" ...
Checkbox field values are only sent at form submission if they're checked, so this will be sent:
[5]
[5, 6]
[6]
Using the naming scheme I've suggested earlier, this would be sent:
[5]
[5, 6]
[1 => 6]
With those field names corrected, you should iterate the $payloads
array using foreach
so that you can get the associative key out as well; this is important, because only checked values are sent with the form.
foreach ($payloads as $key => $payload) {
echo $payload, '<br>';
printf('<td width="50" valign="top">%s</td>', $loadNum[$key]);
printf('<td width="50" valign="top">%s</td>', $unit[$key]);
printf('<td width="50" valign="top">%s</td>', $driver[$key]);
}
Upvotes: 1