doulvyi2172 2015-06-23 01:12
浏览 27
已采纳

php for a array of a array

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>
  • 写回答

1条回答 默认 最新

  • drwurqczo39355510 2015-06-23 01:21
    关注

    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:

    • If only the first checkbox is checked -> [5]
    • If both checkboxes are checked -> [5, 6]
    • If only the second checkbox is checked -> [6]

    Using the naming scheme I've suggested earlier, this would be sent:

    • If only the first checkbox is checked -> [5]
    • If both checkboxes are checked -> [5, 6]
    • If only the second checkbox is checked -> [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]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的