[PHP/JS] Lignes ajoutées dynamiquement ne s'ajoute pas dans $_POST
Bonjour à tous,
je bloque depuis hier sur un petit soucis, j'ai un script JS qui ajoute des lignes dynamiquement dans mon tableau pour pouvoir ajouter des articles à ma commande.
L'index s'incrémente pour alimenter mon tableau qui sera passé dans ma variable $_POST.
Les lignes s'affichent correctement et l'index est incrémenté au moment du clique donc jusqu'ici tout va bien...
Le problème arrive quand j’envoie mon formulaire, seul l'index 0 (donc la ligne qui n'est pas créée par le script) est envoyé dans la variable ! :?
Voici le code de ma page d'ajout de commande :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| <?php
session_start();
include_once('header.php');
include_once('../vars.php');
include_once('../class/commandes.php');
$html = "";
$modals = "";
$formulaire="<form method='POST' action='../controls/addcommandes.php' >";
$formulaire.="<tr>
<td>
<select id='sitecomm' name='site'>";
foreach ($langAgence as $agencecomm) {
$formulaire.= "<option value='" . $agencecomm . "'>" . $agencecomm . "</option>";
}
$formulaire.="</select></td>";
$formulaire.="<td>
<input type='date' name='date' required></td>";
$formulaire.="<td>
<input type='text' name='articles[0][quantite]' required></td>";
$formulaire.="<td>
<select id='periph' name='articles[0][nom]'>";
foreach ($langPeripherique as $peripherique) {
$formulaire.= "<option value='" . $peripherique . "'>" . $peripherique . "</option>";
}
$formulaire.="</select></td>";
$formulaire.="";
$formulaire.="<td>
<div class='input-group'>
<textarea class='form-control' name='note' aria-label='With textarea'></textarea>
</div>
</td>
</tr>
";
?>
<div class="container-fluid">
<table class="table table-striped table-hover table-bordered table align-middle text-center shadow p-3 ">
<thead class="thead">
<tr>
<th>SITE</th>
<th>DATE DE LA DEMANDE</th>
<th>QUANTITÉ</th>
<th>PÉRIPHERIQUE</th>
<th>NOTE</th>
</tr>
<thead>
<tbody>
<?php echo $formulaire ?>
<?php echo $html ?>
</tbody>
</table>
<button type='button' class='btn btn-secondary' id='add-line'>+</button>
<div id='buttons-form-group' class='btn-group float-end' role='group'>
<a href="accueil.php"> <button type="button" class="btn btn-danger square btn-xl">Annuler</button> </a>
<button type='submit' class='btn btn-primary'> Ajouter </button>
</div>
<script>
var currentIndex = 1; // définir la variable currentIndex
// Ajouter un écouteur d'événement click sur le bouton "add-line"
document.getElementById('add-line').addEventListener('click', function() {
// Créer une nouvelle ligne avec les champs de formulaire nécessaires
var newRow = document.createElement("tr");
newRow.innerHTML = `<td></td>
<td></td>
<td><input type='text' name='articles[${currentIndex}][quantite]' ></td>
<td><select name='articles[${currentIndex++}][nom]'>
<?php foreach ($langPeripherique as $peripherique) {
echo "<option value='" . $peripherique . "'>" . $peripherique . "</option>";
} ?>
</select></td>
<td>
</td>`;
// Ajouter la nouvelle ligne à la fin de la balise tbody
document.querySelector('tbody').appendChild(newRow);
});
</script>
</form>
<?php
include_once('footer.php');
?> |
Voici le var_dump($_POST) :
Code:
1 2 3 4 5 6 7 8 9 10 11
| C:\wamp64\www\projet\controls\addcommandes.php:4:
array (size=4)
'site' => string 'GENERAL' (length=7)
'date' => string '2023-01-18' (length=10)
'articles' =>
array (size=1)
0 =>
array (size=2)
'quantite' => string '25' (length=2)
'nom' => string 'ORDINATEURS' (length=11)
'note' => string '' (length=0) |
Ici j'avais deux lignes dans le formulaire : une avec l'index 0 pour 25 ordinateurs et une ajoutée avec l'index 1 contenant 4 téléphones qui ne figure pas dans la variable $_POST.
Je vous serais très reconnaissant si quelqu'un pouvait m'éclairer :mrgreen: