Passing arrays from HTML form to PHP

前端 未结 3 1560
旧时难觅i
旧时难觅i 2021-01-25 18:00

This is the HTML:

 do 
<
相关标签:
3条回答
  • 2021-01-25 18:29

    The indexes are connected automatically, since they're numeric arrays.

    $nvals = count($_REQUEST['shortcut']);
    for ($i = 0; $i < $nvals; $i++) {
      // do something with $_REQUEST['shortcut'][$i] and $_REQUEST['ses'][$i]
    }
    
    0 讨论(0)
  • 2021-01-25 18:39

    You can specify shortcut value as the key and the ses value as the value attribute:

    <input type="text" name="input[a]" value="1" />
    <input type="text" name="input[b]" value="2" />
    <input type="text" name="input[c]" value="3" />
    

    On the server-side you could use a foreach loop to iterate over the array:

    foreach ($_POST['input'] as $shortcut => $ses) {
        // process $shortcut and $ses
    }
    
    0 讨论(0)
  • 2021-01-25 18:50

    Combined array: array_map(null,$_POST['shortcut'],$_POST['ses']);

    But you could of course could foreach over one of the 2, and fetch the other by key.

    Note that if you have elements which may or may not be sent (checkboxes for instance), the only way to keep groups together is to assign them a number beforehand (name=sess[1], name=sess[2], etc.)

    0 讨论(0)
提交回复
热议问题