Bonjour,
j'utilise php,ajax, html et bootstrap pour réaliser une application CRUD.
J'arrive à afficher(dans un tableau), ajouter, modifier et supprimer les données de la base de données.
Le seul souci que j'ai c'est comment rafraichir le contenu du tableau après un ajout, une modification ou une suppression d'un élément.
D'avance merci!
Voici mon code :
Fichier index.php
code permettant l'affichage des données dans un tableau
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 <div class="panel-body"> <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-list" > <thead> <tr> <th style="width:8%">ID</th> <th style="width:30%" title="Date de la remise">Libellé</th> <th style="width:52%">Description</th> <th style="width:10%">Actions</th> </tr> </thead> <tbody id="dataBody" > </tbody> </table> </div>
code ajax pour l'affichage des données:
Fichier ajaxController.php : connexion à la base de données et extraction des données
Code : 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 <script type="text/javascript" > $(document).ready(function() { getExistingData(0,50); // chargement des données lors du chargement de la page }); function getExistingData(start, limit){ $.ajax({ url: 'template/ajaxController.php', method: 'POST', dataType: 'text', data: { key:'getExistingData', start: start, limit:limit }, success: function(response){ if(response != 0){ $("#dataBody").append(response); start +=limit; getExistingData(start, limit); } else{ $('#dataTables-list').DataTable({ responsive: true }); } } }); } </script>
Code php : 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 if($_POST['key']=='getExistingData'){ $start=mysqli_real_escape_string($myConnexion,$_POST['start']); $limit=mysqli_real_escape_string($myConnexion,$_POST['limit']); $query ='SELECT id,libelle,description FROM tsl LIMIT '.$start.','.$limit.''; $result =mysqli_query($myConnexion,$query); if(mysqli_num_rows($result)>0){ $response=""; while($data = mysqli_fetch_array($result,MYSQLI_ASSOC)){ $response.=' <tr class="gradeC" > <td id="line_'.$data["id"].'">'.$data["id"].'</td> <td>'.$data["libelle"].'</td> <td>'.$data["description"].'</td> <td align="center"> <button type="submit" name="detail" href="#formManager" onclick="viewORedit('.$data["id"].',\'edit\')" data-toggle="modal" data-backdrop="false" class="btn btn-primary" style="padding: 0px 10px"> <span class="glyphicon glyphicon-edit"></span> </button> <button type="button" onclick="deleteRow('.$data["id"].')" name="delRow" class="btn btn-danger" style="padding: 0px 10px"> <span class="glyphicon glyphicon-remove"></span> </button> </td> </tr> '; } exit($response); // retourne le resultat de la selection }else{ exit('0'); } }
Partager