Je trouvais une difficulte a affiche mon dernier commentaire par la methode append, j'utilise json qui joue le role entre jQuery et PHP:
index.php:
$('.SubCom').click(function(){
// load image
$('.loading').show();
var commentaire = $('.commentaire').val();
var comLen = commentaire.length;
var dataString = 'commentaire='+ commentaire;
$.ajax({
type: "POST",
url: "Forms/formCommentsCheck.php",
data: dataString,
dataType: "json",
success: function(data) {
// moins de caracteres
if(data.comCheck == "lessChars"){
$('.loading').hide();
$(".comErrors").html("<div>Veuillez entrer plus de caracteres</div>");
} else {
$('.loading').hide();
// append le dernier commentaire
$('.displayNewCom').fadeIn().append(data);
$(".comErrors").html("<div>Merci pour votre commentaire!</div>");
}
}
});
return false;
});
formCommentsCheck.php:<?php
$commentaire = strip_tags($_POST['commentaire'], '<a>');
$date_posted = time();
if(mb_strlen($commentaire, 'utf8') < 10)
{
$comCheck = 'lessChars';
}
else
{
$comCheck = 'goodChars';
$sql = 'INSERT INTO commentaires VALUES (\'\', "'.$commentaire.'")';
$req = $connexion->query($sql);
}
// encodage json
$return_arr["comCheck"] = $comCheck;
echo json_encode($return_arr);
// append data
$sql = 'SELECT * FROM commentaires WHERE ORDER BY idcom DESC LIMIT 1';
//echo $sql;
$req = $connexion->query($sql);
$result = $req->fetchAll();
foreach($result as $kArr=>$vArr)
{
$comments = stripslashes(strip_tags($vArr['comments'], '<a>'));
echo '<div style="background-color:#a5a5f0; overflow:hidden; width:500px;">';
echo '<div style="float:left; width:450px; background-color:#7171e7;">';
echo $comments;
echo '</div>';
echo '<div style="float:left; width:50px; background-color:#3838db;">';
echo '<img src="'.$path2imgs.'noProfile.png" width="50" height="50" />';
echo '</div>';
echo '</div>';
echo '<hr style="width: 500px; color: green;">';
}
?>
alors quand je valide le formulaire l'icone de loading reste afficher, sur la console j'ai ce bout de code:Code : Tout sélectionner
{"comCheck":"lessChars"}<div style="background-color:#a5a5f0; overflow:hidden; width:500px;"><div style="float:left; width:450px; background-color:#7171e7;">fsdf fsdfsdf</div><div style="float:left; width:50px; background-color:#3838db;"><img src="Images/noProfile.png" width="50" height="50" /></div></div><hr style="width: 500px; color: green;">
Code : Tout sélectionner
console.log(data.comCheck);
merci a vous