Bonjour,
J'ai un système de commentaire en ajax.
- Un fichier mur.inc.php où les commentaires s'affichent au dessus du formulaire
- Un fichier add_comment.php pour traiter le formulaire en ajax
Dès qu'on commente un post, le premier s'affiche, jusque là pas de problème.
Mais dès qu'on commente un autre commentaire dans le même post, le commentaire qu'on a écrit en premier s'affiche.
Par exemple :
J'écris un commentaire "com1" avec l'id=1 dans un premier post
Je récris un deuxième commentaire "com2" avec normalement l'id=2
Le commentaire rentre bien dans ma base de donnée mais à l'affichage en ajax, c'est toujours le commentaire avec l'id=1 qui se voit.
Si maintenant je veux écrire "com3" dans un autre post, ça va m'afficher le commentaire avec l'id=1 soit (com1) du premier post
Si on recharge la page manuellement, tout s'affiche normalement.
Je pense que le problème vient de la page add_comment, et du script ajax.
PS : Le script ajax est dessous le formulaire d'envois de commentaire dans la page mur.inc.php
Page : mur.inc.php :Page add_comment.php :
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
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
150
151
152
153
154
155
156
157
158 <div id="container"> <ul class="user-data" style="margin-bottom: 20px;list-style-type: none;"> <?php function getRelativeTime($date) { $date_a_comparer = new DateTime($date); $date_actuelle = new DateTime("now"); $intervalle = $date_a_comparer->diff($date_actuelle); if ($date_a_comparer > $date_actuelle) { $prefixe = 'Dans '; } else { $prefixe = 'Il y a '; } $ans = $intervalle->format('%y'); $mois = $intervalle->format('%m'); $jours = $intervalle->format('%d'); $heures = $intervalle->format('%h'); $minutes = $intervalle->format('%i'); $secondes = $intervalle->format('%s'); if ($ans != 0) { $relative_date = $prefixe . $ans . ' an' . (($ans > 1) ? 's' : ''); if ($mois >= 6) $relative_date .= ' et demi'; } elseif ($mois != 0) { $relative_date = $prefixe . $mois . ' mois'; if ($jours >= 15) $relative_date .= ' et demi'; } elseif ($jours != 0) { $relative_date = $prefixe . $jours . ' jour' . (($jours > 1) ? 's' : ''); } elseif ($heures != 0) { $relative_date = $prefixe . $heures . ' heure' . (($heures > 1) ? 's' : ''); } elseif ($minutes != 0) { $relative_date = $prefixe . $minutes . ' minute' . (($minutes > 1) ? 's' : ''); } else { $relative_date = $prefixe . ' quelques secondes'; } return $relative_date; } try{ $bdd = new PDO('mysql:host=sql2.cluster1.easy-hebergement.net;dbname=webastronomiea', 'webastronomiea', 'nico181290'); } catch (PDOException $e){ die('Erreur : ' . $e->getMessage()); } $obj = $bdd->query("SELECT * FROM wa_mur, wa_communaute WHERE id_membre = wa_communaute.id ORDER BY date DESC;"); $req = $bdd->prepare("SELECT * FROM wa_comment, wa_communaute WHERE post_id = :id AND membre_id = wa_communaute.id ORDER BY id_com ASC;"); $is_empty = true; while ($data = $obj->fetch()) { $is_empty = false; ?> <li class="user-item" style="float: left;padding: 13px 10px 10px;width: 460px;border-top: 1px solid #cecece;word-wrap: break-word;"> <div class="user-avatar float-l" style="margin-right: 10px;"> <a href="/<?php echo htmlentities(trim($data['pseudo'])); ?>"><img src="../img/upload/<?php echo htmlentities(trim($data['id'])); ?>-mini.png" width="50" height="50" alt="" /></a> </div> <div class="user-title" style="margin-bottom: 5px;font-weight: bold;"> <a href="/<?php echo htmlentities(trim($data['pseudo'])); ?>" style="color: #005B83;"><?php echo htmlentities(trim($data['prenom'])); ?> <?php echo htmlentities(trim($data['nom'])); ?></a> </div> <div class="user-content" style="margin-left: 60px;"> <p><?php echo htmlentities(nl2br(trim($data['poster']))); ?></p> </div> <div class="sub-userContent" style="margin-left: 60px;"> <div class="sub-userFooter" style="margin-top: 6px;"> <p class="size11" style="color: #777;"><a href="" class="sub-userFooter-link" style="color: #0D5085;">J'aime</a> - <a href="" class="sub-userFooter-link" style="color: #0D5085;">Commenter</a> - <?php echo getRelativeTime($data['date']); ?></p> </div> <ul class="sub-userData" style="list-style-type: none;"> <li> <i class="sub-userArrow" style="background: url(../img/top-sub-user.png);background-repeat: no-repeat;height: 5px;display: block;margin-top: 5px;margin-left: 60px;"></i> </li> <?php $req->execute(array('id'=>$data['id_post'])); while ($data_comm = $req->fetch()) { ?> <li class="sub-userItem" style="background-color: #bebebe;margin: 0;padding: 4px;margin: 0 0 1px;min-height: 32px;"> <div class="user-avatarCom" style="float: left;padding: 0;margin: 0;"> <img style="margin-right: 4px;" src="../img/upload/<?php echo $data_comm['membre_id']; ?>-mini.png" width="32" height="32" alt="" /> </div> <div class="sub-userCom" style="overflow: hidden;padding-left: 4px;"> <div> <p class="size11"> <a href="/<?php echo htmlentities(trim($data_comm['pseudo'])); ?>" style="color: #005B83;"> <b><?php echo htmlentities(trim($data_comm['prenom'])); ?> <?php echo htmlentities(trim($data_comm['nom'])); ?></b></a> <span> <?php echo nl2br(htmlspecialchars($data_comm['msg_com'])); ?></span> </p> </div> <div> <p class="size11"><span style="color: #666;display: block;margin-top: 4px;"><?php echo getRelativeTime($data_comm['date']); ?></span></p> </div> </div> </li> <? } ?> <div class="<?php echo $data['id']; ?>"></div> <li class="sub-userItem" style="background-color: #bebebe;margin: 0;padding: 4px;min-height: 32px;"> <div class="user-avatarCom" style="float: left;padding: 0;margin: 0;"> <img style="margin-right: 4px;" src="../img/upload/<?php echo $result['id']; ?>-mini.png" width="32" height="32" alt="" /> </div> <form action="" method="post" onsubmit="return false;" class="form_com"> <input type="hidden" name="post_id" value="<?php echo $data['id_post']; ?>" /> <textarea name="msg_com" class="<?php echo $data['pseudo']; ?>" style="border: 1px solid #999;font-size: 11px; margin-left: 5px; height: 26px; padding: 5px; width: 343px;resize: none;font-family: Tahoma;position: relative;overflow: hidden;display: block;" id="<?php echo $data['pseudo']; ?>" placeholder="Écrire un commentaire"></textarea> </form> <script type="text/javascript"> var J = jQuery.noConflict(); J(document).ready(function(){ J(".form_com").keypress(function(e) { if(e.which == 13) { var <?php echo $data['pseudo']; ?> = J('.<?php echo $data['pseudo']; ?>').val(); if( <?php echo $data['pseudo']; ?> == '') { return false; } else { J('span.load-refresh').show(); J.ajax({ url: "ajax/add_comment.php", type: "POST", cache: false, data: J(this).serialize(), success: function(html) { J('.<?php echo $data['id']; ?>').append(html); J('.<?php echo $data['pseudo']; ?>').val(''); J('span.load-refresh').hide(); } }); } return false; } }); }); </script> <script type="text/javascript">window.addEvent('domready', function() {new DynamicTextarea('<?php echo $data['pseudo']; ?>');});</script> </li> </ul> </div> </li> <?php $req->closeCursor(); if ($is_empty) echo '<div style="text-align: center;font-size: 11px;color: #666;">Aucune actualités pour l\'instant</div>'; } ?> </ul> </div>Merci beaucoup pour votre aide
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
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 <?php session_start(); require('../include/functions.inc.php'); function getRelativeTime($date) { $date_a_comparer = new DateTime($date); $date_actuelle = new DateTime("now"); $intervalle = $date_a_comparer->diff($date_actuelle); if ($date_a_comparer > $date_actuelle) { $prefixe = 'Dans '; } else { $prefixe = 'Il y a '; } $ans = $intervalle->format('%y'); $mois = $intervalle->format('%m'); $jours = $intervalle->format('%d'); $heures = $intervalle->format('%h'); $minutes = $intervalle->format('%i'); $secondes = $intervalle->format('%s'); if ($ans != 0) { $relative_date = $prefixe . $ans . ' an' . (($ans > 1) ? 's' : ''); if ($mois >= 6) $relative_date .= ' et demi'; } elseif ($mois != 0) { $relative_date = $prefixe . $mois . ' mois'; if ($jours >= 15) $relative_date .= ' et demi'; } elseif ($jours != 0) { $relative_date = $prefixe . $jours . ' jour' . (($jours > 1) ? 's' : ''); } elseif ($heures != 0) { $relative_date = $prefixe . $heures . ' heure' . (($heures > 1) ? 's' : ''); } elseif ($minutes != 0) { $relative_date = $prefixe . $minutes . ' minute' . (($minutes > 1) ? 's' : ''); } else { $relative_date = $prefixe . ' quelques secondes'; } return $relative_date; } $sql = connect_sql(); $pseudo_membre = $_SESSION['pseudo']; $requete = mysql_query("SELECT * FROM wa_communaute WHERE pseudo='".$pseudo_membre."'"); $result = mysql_fetch_assoc ($requete); $post_id = $_POST['post_id']; $msg_com = $_POST['msg_com']; if(get_magic_quotes_gpc()) { $msg_com = stripslashes($msg_com); } mysql_query('INSERT INTO wa_comment (id_com, membre_id, msg_com, post_id, date) values("", "'.$_SESSION['id'].'", "'.mysql_real_escape_string(trim(htmlentities($msg_com, ENT_QUOTES, 'UTF-8'))).'", "'.$post_id.'", "now()")'); mysql_close(); try{ $bdd = new PDO('mysql:host=sql2.cluster1.easy-hebergement.net;dbname=webastronomiea', 'webastronomiea', 'nico181290'); } catch (PDOException $e){ die('Erreur : ' . $e->getMessage()); } $obj = $bdd->query("SELECT * FROM wa_mur, wa_communaute WHERE id_membre = wa_communaute.id ORDER BY date DESC;"); $req = $bdd->prepare("SELECT * FROM wa_comment, wa_communaute WHERE post_id = :id AND membre_id = wa_communaute.id ORDER BY date DESC;"); $data = $obj->fetch(); $req->execute(array('id'=>$data['id_post'])); $data_comm = $req->fetch(); ?> <li class="sub-userItem" style="background-color: #bebebe;margin: 0;padding: 4px;margin: 0 0 1px;min-height: 32px;"> <div class="user-avatarCom" style="float: left;padding: 0;margin: 0;"> <img style="margin-right: 4px;" src="../img/upload/<?php echo $data_comm['membre_id']; ?>-mini.png" width="32" height="32" alt="" /> </div> <div class="sub-userCom" style="overflow: hidden;padding-left: 4px;"> <div> <p class="size11"> <a href="/<?php echo htmlentities(trim($data_comm['pseudo'])); ?>" style="color: #005B83;"> <b><?php echo htmlentities(trim($data_comm['prenom'])); ?> <?php echo htmlentities(trim($data_comm['nom'])); ?></b></a> <span> <?php echo nl2br(htmlspecialchars($data_comm['msg_com'])); ?></span> </p> </div> <div> <p class="size11"><span style="color: #666;display: block;margin-top: 4px;"><?php echo getRelativeTime($data_comm['date']); ?></span></p> </div> </div> </li>
Partager