Bonjour,
J'ai le système suivant :
Principe : je sélectionne un élément dans la colonne "Ref", et grâce à de l'AJAX, lors de cette sélection, je charge le nom du produit dans la colonne "Ref Article" qui lui est attribué dans la bdd.
Le bouton "Ajouter un produit" ajoute une ligne, et celle d'à côté la supprime.
Mon problème: Lorsque je créé plusieurs lignes, le "Ref Article" invoqué vient se mettre sur la première ligne, ce qui est normal car je n'ai pas spécifié d'id de ligne à mon ajax. Comment m'y prendre?
voici mon code, merci d'avance :
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
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 <button id="add_article" class="btn btn-success">Ajouter un produit</button> <button id="del_article" class="btn btn-danger">Supprimer le(s) produit(s)</button> <table class="table table-bordered table-striped table-condensed"> <thead> <tr> <th width="2"></th> <th width="80">Ref</th> <th width="20">Ref Article</th> </tr> </thead> <tbody id="table_article"> </tbody> </table> <!-- lignes ajoutés au tableau --> <table id="fantome" style="display:none"> <tbody> <tr> <td><input type="checkbox" name="chkbox[]"></td> <td> <select type="text" class="form-control scope article_ref" style="width: 100%;" onchange="ref_to_rearticle(this.value)"> <option></option> <?php $list = $bdd->query('SELECT * FROM produit WHERE produit_contact = "'.$_SESSION['Contact_id'].'" ORDER BY `produit_ref` ASC'); while ($data = $list->fetch()) { ?> <option value="<?php echo $data['produit_ref']; ?>"><?php echo $data['produit_ref']; ?></option> <?php } $list->closeCursor(); ?> </select> </td> <td> <div id="article_refarticle"> Le produit sera chargé ici </div> </td> </tr> </table>
Code JavaScript : Sélectionner tout - Visualiser dans une fenêtre à part
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 function ref_to_rearticle(produit_ref) { $.ajax({ url: "modal_header/Actions/inputdevis.php", method: "POST", data: { article_ref: produit_ref } }) .done(function( retour_html ) { $("#article_refarticle").html( retour_html ); }) .fail(function() { alert( "error facture creation ajax" ); }); } // AJOUTER UN ARTICLE DANS LE DEVIS $(document).ready(function() { var Table_noms_messages = document.getElementById('table_article'); const TR_Base = document.querySelector('#fantome tbody tr'); $('#add_article').click(function () { // "Ajouter un produit" var new_tr_clone = TR_Base.cloneNode(true); Table_noms_messages.appendChild( new_tr_clone ); //select 2 $( new_tr_clone ).find("select.form-control").select2({ tags: true, width: 'resolve' }); }); $('#del_article').click(function() { try { var rowCount = Table_noms_messages.rows.length; for(let i=0; i<rowCount; i++) { let ligneT = Table_noms_messages.rows[i], chkbox = ligneT.cells[0].childNodes[0] ; if(chkbox!=null && chkbox.checked) { Table_noms_messages.deleteRow(i); rowCount--; i--; } } } catch(e) { alert(e); } });
inputdevis.php (l'ajax quoi):
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <?php include '../../../Include/connect.php' ; $article_ref = (array_key_exists('article_ref', $_POST)) ? $_POST['article_ref']: NULL; if ($article_ref) { $stmt = $bdd->prepare(' SELECT * FROM produit WHERE produit_ref = :ref '); $stmt->bindValue(':ref', strtoupper($article_ref), PDO::PARAM_STR); $stmt->execute(); if ($donnees = $stmt->fetch(PDO::FETCH_ASSOC)) { ?> <input class="form-control" type="text" value="<?php echo $donnees['produit_nom'] ?>" /> } ?>
Merci d'avance








Répondre avec citation



Partager