Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array in value in input type checkbox?

Tags:

arrays

html

php

<form action="next.php" method="post">
<table>
    <tr>
      <td>Paul</td>
      <td>Attuck</td>
      <td>[email protected]</td>
      <td><input type="checkbox" name="list[]" value="[email protected]" /></td>
    </tr>
    <tr>
      <td>James</td>
      <td>Bond</td>
      <td>[email protected]</td>
      <td><input type="checkbox" name="list[]" value="[email protected]" /></td>
    </tr>
    <tr>
      <td>Name</td>
      <td>Last</td>
      <td>[email protected]</td>
      <td><input type="checkbox" name="list[]" value="[email protected]" /></td>
    </tr>
</table>
    <input type="submit" name="submit" value="submit" />
</form>

and if i checked all checkbox and send form to next.php i can:

print_r($_POST['tags']);
// output
Array
(
    [0] => [email protected]
    [1] => [email protected]
    [2] => [email protected]
)

How can i make:

Array
    (
        [0] => array ([0] => Paul
                      [1] => Attuck
                      [2] => [email protected])
        [1] => array ([0] => James
                      [1] => Bond
                      [2] => [email protected])
        [2] => array ([0] => Last
                      [1] => Name
                      [2] => [email protected])
    )

? I try use serialize but this make " - dont can use this in html form.

like image 654
Paul Attuck Avatar asked Jan 24 '26 06:01

Paul Attuck


1 Answers

If you make sure there is one character forbidden in the name fields (I use | here), you can do

<tr>
      <td>Paul</td>
      <td>Attuck</td>
      <td>[email protected]</td>
      <td><input type="checkbox" name="list[]" value="Paul|Attuck|[email protected]" /></td>
</tr>

And will get

print_r($_POST['tags']);
// output
Array
(
    [0] => Paul|Attuck|[email protected]
    [1] => James|Bond|[email protected]
    [2] => Last|Name|[email protected]
)

Which you can transform by

$names=array();
foreach ($_POST['tags']) as $tag)
  $names[]=explode('|',$tag,3);
like image 123
Eugen Rieck Avatar answered Jan 25 '26 19:01

Eugen Rieck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!