Bonjour,
Je souhaite ajouter une entrée dans une table SQL avec les informations contenues dans le formulaire mais lors du clic sur le bouton de validation, aucune action ne se produit. Pouvez-vous m'aider à trouver d'où viens l'erreur ?
Fichier Locataire.php:
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 <html> <head> <title>Gestion locataire</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link rel="stylesheet" href="style.css" /> <script> $(document).ready(function(){ fetchUser(); function fetchUser() { var actionlocataire = "Load"; $.ajax({ url : "actionlocataire.php", method:"POST", data:{actionlocataire:actionlocataire}, success:function(data){ $('#result').html(data); } }); } //Pour ajouter un actionlocataire $('#actionlocataire').click(function(){ var loc_id = $('#loc_id').val(); var loc_nom = $('#loc_nom').val(); var loc_nomentier = $('#loc_nomentier').val(); var loc_debut = $('#loc_debut').val(); var loc_fin = $('#loc_fin').val(); var loc_caf = $('#loc_caf').val(); var loc_tel = $('#loc_tel').val(); var loc_mail = $('#loc_mail').val(); var actionlocataire = $('#actionlocataire').val(); if(loc_nomentier != '' && loc_debut != '' loc_caf != '' loc_tel != '' loc_mail != '') //Choisir les champs obligatoires (par defaut tous sauf les dates) { $.ajax({ url : "actionlocataire.php", method:"POST", data:{loc_nom:loc_nom, loc_nomentier:loc_nomentier, loc_debut:loc_debut, loc_fin:loc_fin, loc_caf:loc_caf, loc_tel:loc_tel, loc_mail:loc_mail, datearchivage:datearchivage}, //Envoie les donnees success:function(data){ alert(data); $('#logementform').modal('hide'); //Pour cacher le formulaire locataire apres envoi fetchUser(); } }); } else { alert("Les champs sont obligatoires"); } }); </script> </head> <body> <body> <div id="top"> <a href="index.php">Gestion des Locataires</a> <div class="bouton_menu">☰</div> <div id="menu"> <td><li> <a href="index.php">Accueil</a> <button type="button" id="modal_button" class="bouton_creer" onclick="Ouvrir()">LOCATAIRES</button> <a href="logement.php">LOGEMENTS</a> </li></td> </div> </div> <div id="formulaire"> <div class="modal-header"> <h4 class="modal-title">Creer un Locataire</h4> </div> <div class="modal-body"> <label>Nom du Locataire</label> <input type="text" name="loc_nom" id="loc_nom" class="form-control" pattern="[A-Za-z]{1,25}" /> <br /> <label>Nom Entier du Locataire</label> <input type="text" name="loc_nomentier" id="loc_nomentier" class="form-control" pattern="[A-Za-z]{1,25}" /> <br /> <label>Debut de Location</label> <input type="text" name="loc_debut" id="loc_debut" class="form-control" pattern="[A-Za-z]{1,25}" /> <br /> <label>Fin de Location</label> <input type="text" name="loc_fin" id="loc_fin" class="form-control" pattern="[A-Za-z]{1,25}"/> <br /> <label>CAF</label> <input type="text" name="loc_caf" id="loc_caf" class="form-control" pattern="[A-Za-z]{1,25}"/> <br /> <label>Telephone</label> <input type="text" name="loc_tel" id="loc_tel" class="form-control" pattern="[A-Za-z]{1,25}"/> <br /> <label>Courriel</label> <input type="text" name="loc_mail" id="loc_mail" class="form-control" pattern="[A-Za-z]{1,25}"/> <br /> </div> <div class="modal-footer"> <input type="hidden" name="loc_id" id="loc_id" /> <input type="submit" class="btn btn-success" name="actionlocataire" id="actionlocataire" /> <button type="button" class="btn btn-default" data-dismiss="modal" onclick="Fermer()">Fermer</button> </div> </div> <div id="content"> <div id="result"> <!-- Data will load under this tag!--> </div> </div> <script> function Fermer() { document.getElementById("formulaire").style.display = "none"; } </script> <script> function Ouvrir() { document.getElementById("formulaire").style.display = "inline-block"; } </script> <script type="text/javascript"> if(window.innerWidth<800) { document.querySelector("#formulaire").style.display="none"; } </script> <script type="text/javascript"> if(window.innerWidth>800) { document.querySelector("#content").style.width="60%"; document.querySelector("#content").style.float="left" document.querySelector("#formulaire").style.width="27%" } </script> <script type="text/javascript"> if(window.innerWidth>800) { document.querySelector("#formulaire").style.display="inline-block"; } </script> </body> </html> </body> </html>
Fichier actionlocataire.php:
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 <?php //Database connection en PDO $username = 'root'; $password = ''; $connection = new PDO('mysql:host=localhost;port=3306;dbname=testsql;charset=utf8','root', '', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false ]); //POUR CREER UN NOUVEAU LOCATAIRE if(isset($_POST["actionlocataire"])) //METHODE POST DU FORMULAIRE { //For charger les donnees if($_POST["actionlocataire"] == "Load") { $statement = $connection->prepare("SELECT * FROM locataire"); $statement->execute(); $result = $statement->fetchAll(); $output = ''; $output .= ' '; if($statement->rowCount() > 0) { foreach($result as $row) { // VOTRE TABLEAU DE RESULTAT $output .= ' <tr> <td>'.$row["loc_nom"].'</td> <td>'.$row["loc_nomentier"].'</td> <td>'.$row["loc_debut"].'</td> <td>'.$row["loc_fin"].'</td> <td>'.$row["loc_caf"].'</td> <td>'.$row["loc_tel"].'</td> <td>'.$row["loc_mail"].'</td> <td><center><button type="button" loc_id="'.$row["loc_id"].'" class="btn btn-warning update" onclick="Ouvrir()">Update</button></center></td> <td><center><button type="button" loc_id="'.$row["loc_id"].'" class="btn btn-danger delete">Delete</button></center></td> </tr> '; } } else { $output .= ' <tr> <td align="center">Erreur !</td> </tr> '; } $output .= '</table></div>'; echo $output; } //POUR CREER UN NOUVEAU LOCATAIRE if($_POST["actionlocataire"] == "Creer") { $statement = $connection->prepare(" INSERT INTO locataire (loc_nom, loc_nomentier, loc_debut, loc_fin, loc_caf, loc_tel, loc_mail) VALUES (:loc_nom, :loc_nomentier, :loc_debut, :loc_fin, :loc_caf, :loc_tel, :loc_mail) "); $result = $statement->execute( array( ':loc_nom' => $_POST["loc_nom"], ':loc_nomentier' => $_POST["loc_nomentier"], ':loc_debut' => $_POST["loc_debut"], ':loc_fin' => $_POST["loc_fin"] ':loc_caf' => $_POST["loc_caf"] ':loc_tel' => $_POST["loc_tel"] ':loc_mail' => $_POST["loc_mail"] ) ); if(!empty($result)) { echo 'Locataire ajoute avec succes'; } } //Pour nafficher quun LOCATAIRE if($_POST["actionlocataire"] == "Select") { $output = array(); $statement = $connection->prepare( "SELECT * FROM locataire WHERE loc_id = '".$_POST["loc_id"]."' LIMIT 1" ); $statement->execute(); $result = $statement->fetchAll(); foreach($result as $row) { $output["loc_nom"] = $row["loc_nom"]; $output["loc_nomentier"] = $row["loc_nomentier"]; $output["loc_debut"] = $row["loc_debut"]; $output["loc_fin"] = $row["loc_fin"]; $output["loc_caf"] = $row["loc_caf"]; $output["loc_tel"] = $row["loc_tel"]; $output["loc_mail"] = $row["loc_mail"]; } echo json_encode($output); } if($_POST["actionlocataire"] == "Update") { $statement = $connection->prepare( "UPDATE logement SET loc_nom = :loc_nom, loc_nomentier = :loc_nomentier, loc_debut = :loc_debut, loc_fin = :loc_fin, loc_caf = :loc_caf, loc_tel = :loc_tel, loc_mail = :loc_mail WHERE loc_id = :loc_id " ); $result = $statement->execute( array( ':loc_nom' => $_POST["loc_nom"], ':loc_nomentier' => $_POST["loc_nomentier"], ':loc_debut' => $_POST["loc_debut"], ':loc_fin' => $_POST["loc_fin"], ':loc_caf' => $_POST["loc_caf"], ':loc_tel' => $_POST["loc_tel"], ':loc_mail' => $_POST["loc_mail"], ':loc_id' => $_POST["loc_id"] ) ); if(!empty($result)) { echo 'Locataire mis a jour'; } } if($_POST["actionlocataire"] == "Delete") { $statement = $connection->prepare( "DELETE FROM locataire WHERE loc_id = :loc_id" ); $result = $statement->execute( array( ':loc_id' => $_POST["loc_id"] ) ); if(!empty($result)) { echo 'Locataire supprime'; } } } ?>
Merci d'avance pour votre aide
Partager