5546 sujets

Sémantique web et HTML

Bonjour à tous
Je ne me souviens plus comment coder les attributs "name" des <input> pour obtenir le tableau que je voudrais du côté serveur
La structure du <form> est la suivante:

<form method="post" action="file-replace-options.php">
    <table>
        <tr>
            <td><input type="checkbox" name="fromCheck[]"></td>
            <td><input type="text" name="fromPath[]" value="aaaaaaa"></td>
            <td><input type="checkbox" name="toCheck[]"></td>
            <td><input type="text" name="toPath[]" value="bbbbbbb"></td>
        <tr>
        <tr>
            <td><input type="checkbox" name="fromCheck[]"></td>
            <td><input type="text" name="fromPath[]" value="cccccccc"></td>
            <td><input type="checkbox" name="toCheck[]"></td>
            <td><input type="text" name="toPath[]" value="dddddddd"></td>
        <tr>
        <tr>
            <td><input type="checkbox" name="fromCheck[]"></td>
            <td><input type="text" name="fromPath[]" value="eeeeeeee"></td>
            <td><input type="checkbox" name="toCheck[]"></td>
            <td><input type="text" name="toPath[]" value="ffffffff"></td>
        <tr>
        <td colspan="4" style="text-align:center"><input type="submit" value="OK"></td></tr>
    </table>
</form>

Si je code de cette façon, la variable $_POST du serveur contient:

Array
(
    [fromCheck] => Array
        (
            [0] => on
            [1] => on
        )

    [fromPath] => Array
        (
            [0] => aaaaaaa
            [1] => cccccccc
            [2] => eeeeeeee
        )

    [toPath] => Array
        (
            [0] => bbbbbbb
            [1] => dddddddd
            [2] => ffffffff
        )

    [toCheck] => Array
        (
            [0] => on
            [1] => on
        )
)

Ce que je voudrais obtenir, c'est :

Array
(
	[0] => Array
	  (
		[fromCheck] => on
        [fromPath] => aaaaaaa
		[toPath] => bbbbbbb
      )
	[1] => Array
	  (
	     [fromPath] => cccccccc
		 [toCheck] => on
		 [toPath] => dddddddd
      )
	[2] => Array
	  (
         [fromCheck] => on
		 [fromPath] => eeeeeeee
		 [toCheck] => on
		 [toPath] => ffffffff
       )
)

Je me souviens avoir réalisé de choses de ce genre dans le passé en jouant sur l'attribut "name" des <input> mais j'ai oublié comment faire et je n'y arrive plus.

Merci de me rafraichir les méninges!
Modifié par PapyJP (25 Oct 2017 - 11:03)
Salut JP Smiley smile

tu veux faire ceci :

name= mesinfos[]['fromcheck']
name= mesinfos[]['fromPath']

etc etc

au bout tu récupère l'array "mesinfos" qui contiendra ce que tu veux Smiley cligne
Eh bien non! ce n'est pas ça!
D'une part les guillemets sont inutiles, d'autre part ça ne regroupe pas les éléments par ligne.
Voici le résultat:

Array
(
    [mesinfos] => Array
        (
            [0] => Array
                (
                    ['fromPath'] => aaaaaa
                )

            [1] => Array
                (
                    ['toCheck'] => on
                )

            [2] => Array
                (
                    ['toPath'] => bbbbbb
                )

            [3] => Array
                (
                    ['fromCheck'] => on
                )

            [4] => Array
                (
                    ['fromPath'] => cccccc
                )

            [5] => Array
                (
                    ['toPath'] => dddddd
                )

            [6] => Array
                (
                    ['fromCheck'] => on
                )

            [7] => Array
                (
                    ['fromPath'] => eeeeee
                )

            [8] => Array
                (
                    ['toCheck'] => on
                )

            [9] => Array
                (
                    ['toPath'] => ffffff
                )

        )

)

Modifié par PapyJP (25 Oct 2017 - 13:14)
Il faudrait faire :

name="data[0]['fromCheck']"


(passer 0 à 1 pour le 2ème groupe et ainsi de suite)
Meilleure solution
Merci
Effectivement ça marche, mais j'espérais que data[] serait suffisant.
Manifestement ce n'est pas le cas, toute nouvelle occurrence de data|] incrémente l'index.
Modérateur
yep,

mais c'est ainsi que php fonctionne:
si je déclare:

$truc = array();

$truc[]['fromCheck'] = true;
$truc[]['toPath'] = true;
$truc[]['fromPath'] = true;
$truc[]['fromCheck'] = true;

PHP n'a aucune raison de regrouper les trois premières occurrences sous le même index.

Pour php «[]» signifie ajouter à la fin du tableau.
kustolovic a écrit :
yep,
Pour php «[]» signifie ajouter à la fin du tableau.

Certes, mais c'est ch...!
Bonne nuit