Bonjour,
Voila je rencontre un petit problème avec mon code.
J'ai créer un formulaire dans un modal qui me permet de rajouter un membre, le traitement d'ajout de membre est réalisé dans un script php avec un retour ajax, le retour me retourne une "Notification" qui m'indique si un des éléments n'est pas bon, et est sensée me renvoyé une notification quand l'insertion en BDD est OK,
==> l'affichage
<?php
require_once __DIR__ . '/assets/config/bootstrap.php';
require_once __DIR__ . '/assets/config/functions_global_admin.php';
require_once __DIR__ . '/assets/functions/team_functions.php';
$page_title ='Team';
include __DIR__. '/assets/includes/header_admin.php';
?>
<?php include __DIR__.'/assets/includes/flash.php';?>
<section class="recent">
<div class="team__grid">
<div class="team__card">
<div class="card__header">
<h3>All Team</h3>
<button id="add_team_member">
<i class="fas fa-user-plus"></i>
Ajouter
</button>
</div>
<div class="table-responsive">
<table>
<thead>
<tr>
<th>Nom</th>
<th>Prénom</th>
<th>Photo</th>
<th>Email</th>
<th>Status</th>
<th>Confirmation</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<div class="resultat">
<tbody>
<?php while ($member = $Allmembres->fetch() ): ?>
<?php
// changement format date
$date = str_replace('/', '-', $member['date_enregistrement']);
//récupération de la photo de profil
$id_photo = $member['photo_id'];
$data = $pdo->query("SELECT * FROM photo WHERE id_photo = '$id_photo'");
$photo = $data->fetch(PDO::FETCH_ASSOC);
?>
<tr>
<td><?=$member['nom']?></td>
<td><?=$member['prenom']?></td>
<td class="td-team">
<?php
if ($member['photo_id'] == NULL) {
if($member['civilite'] == 0) {
echo "<div class='img-profil' style='background-image: url(assets/photos/male.svg)'></div>";
}elseif($member['civilite'] == 1){
echo "<div class='img-profil' style='background-image: url(assets/photos/female.svg)'></div>";
}else{
echo "<div class='img-profil' style='background-image: url(assets/photos/profil.svg)'></div>";
}
}else{
echo "<div class='img-profil' style='background-image: url(assets/photos/ " .$photo['profil']. " )'></div>";
}
?>
</td>
<td><a href="mailto:<?=$member['email']?>" class="email_member"><?=$member['email']?></a></td>
<td>
<?php if ($member['statut'] == 0):?>
<?= '<p class="badge admin">Admin</p>';?>
<?php elseif($member['statut'] == 1) :?>
<?= '<p class="badge user">User</p>';?>
<?php else :?>
<?= '<p class="badge editer">Editeur</p>';?>
<?php endif;?>
</td>
<td>
<?php if ($member['confirmation'] == 0):?>
<?= '<p class="badge danger confirmation">Non</p>';?>
<?php else:?>
<?= '<p class="badge success confirmation">Oui</p>';?>
<?php endif;?>
</td>
<td><?= date('d-m-Y', strtotime($date))?> </td>
<td></td>
</tr>
<?php endwhile; ?>
</tbody>
</div><!-- fin div résultat ajax-->
</table>
</div>
</div>
</div>
<div class="pagination">
<nav aria-label="...">
<ul class="">
<?php
for($i=1;$i<=$pageTotales;$i++){
if($i == $pageCourante){
echo '<li class=" active" aria-current="page"><span class="page-link">'.$i.'<span class="sr-only">(current)</span></span></li>';
}else{
echo'<li class=""><a class="page-link" href="team.php?page='.$i.'">'.$i.'</a></li> ';
}
}
?>
</ul>
</nav>
</div>
</section>
<div class="modal-bg"> <!-- modal d'ajout de membre -->
<div class="modal">
<span class="modal-close">X</span>
<div class="modal__title">
<h2>Ajouter un membre</h2>
</div>
<form>
<div>
<label class="" for="statut">Civilité :</label>
<select class="custom-select" name="civilite" id="member_civilite">
<option> ... </option>
<option value="<?= FEMME ?>">Madame</option>
<option value="<?= HOMME ?>">Monsieur</option>
<option value="<?= AUTRE ?>">Autre</option>
</select>
</div>
<label for="name">Nom: </label>
<input type="text" name="name">
<label for="name">Prenom: </label>
<input type="text" name="prenom">
<label for="name">Email: </label>
<input type="email" name="email">
<div>
<label class="" for="statut">Statut :</label>
<select class="custom-select" name="statut" id="member_statut">
<option> ... </option>
<option value="<?= ROLE_ADMIN ?>">Admin</option>
<option value="<?= ROLE_USER ?>">User</option>
<option value="<?= ROLE_EDITEUR ?>">Editeur</option>
</select>
</div>
<div class="modal__footer">
<input type="submit" class="modal_submit" id="add_team_member" name="add_team_member" value="Valider" >
</div>
</form>
</div>
</div>
<?php
include __DIR__. '/assets/includes/footer_admin.php';
?>
==> le traitement Ajax:
$(document).ready(function(){
// add team member
$(function(){
$(" form #add_team_member").click(function (e) {
e.preventDefault();
var $form = $(this).closest('form');
var civilite = $form.find('#member_civilite').val();
var memberName = $form.find('input[name="name"]').val();
var memberPrenom = $form.find('input[name="prenom"]').val();
var memberEmail = $form.find('input[name="email"]').val();
var statut = $form.find('#member_statut').val();
var parameters = "civilite="+civilite+"&name=" + memberName + "&prenom=" + memberPrenom + '&email=' + memberEmail + "&statut="+ statut;
$.ajax({
url:'assets/script/add_team_member.php',
method : 'post',
data: parameters,
dataType: 'JSON',
success: function (data) {
if (data.status == 'success') {
$('.modal-bg').removeClass('active');
console.log(data.status)
retour = $('.resultat').html(data.resultat);
return retour;
} else {
console.log(data.status)
retour = $('.resultat').html(data.resultat);
return retour;
}
}
});
});
});
});
==> le traitement en PHP
<?php
require_once __DIR__ . '/../config/bootstrap.php';
require_once __DIR__ . '/../functions/team_functions.php';
$msg = '';
$status = '';
if(!preg_match('~^[a-zA-Z-]+$~',$_POST['name'])){
$status = 'error';
$msg = 'Nom manquant';
}elseif (!preg_match('~^[a-zA-Z-]+$~',$_POST['prenom'])) {
$status = 'error';
$msg = 'Prénom manquant';
}elseif(getMembreBy($pdo, 'email', $_POST['email'])!==null){
$status = 'error';
$msg = 'Email déja utilisé !';
}elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$status = 'error';
$msg = 'Email non valide ou manquant!';
}else{
//création d'un mot de passe aléatoire
function passgen($nbChar) {
$chaine ="mnoTUzS5678kVvwxy9WXYZRNCDEFrslq41GtuaHIJKpOPQA23LcdefghiBMbj0@#";
srand((double)microtime()*1000000);
$pass = '';
for($i=0; $i<$nbChar; $i++){
$pass .= $chaine[rand()%strlen($chaine)];
}
return $pass;
}
$mdp = passgen(8);
$hash = password_hash($mdp, PASSWORD_DEFAULT);
//création de l'username + création du name
$first_name = $_POST['prenom'];
$a = $first_name[0];
$explode_name = explode(' ',$_POST['name']);
$explode_fn = explode(' ',$_POST['first_name']);
$username = $a.$explode_name[0];
$name = 'Fr'.$explode_fn[0].$explode_name[0].bin2hex(random_bytes(6));
//autres valeurs
$token = bin2hex(random_bytes(16));
$civilite = $_POST['civilite'];
$statut = $_POST['statut'];
// requete SQL
$req = $pdo->prepare(
'INSERT INTO team (
civilite,
username,
nom,
prenom,
email,
password,
photo_id,
statut,
date_enregistrement,
confirmation,
token,
name
)
VALUES (
:civilite,
:username,
:nom,
:prenom,
:email,
:password,
:photo_id,
:statut,
:date,
:confirmation,
:token,
:name
)'
);
$req->bindParam(':civilite',$civilite);
$req->bindParam(':username',$username);
$req->bindParam(':nom',htmlspecialchars($_POST['name']));
$req->bindParam(':prenom',htmlspecialchars($_POST['prenom']));
$req->bindParam(':email',$_POST['email']);
$req->bindParam(':password',$hash);
$req->bindValue(':photo_id',NULL);
$req->bindValue(':statut',$statut);
$req->bindValue(':date',(new DateTime())->format('Y-m-d H:i:s'));
$req->bindValue(':confirmation',0);
$req->bindParam(':token',$token);
$req->bindParam(':name',$name);
$req->execute();
if($req){
$status = 'success';
$msg = 'Membre ajouté';
}
}
$resultat = '';
$resultat .= '<div id="toats" class="notif alert-' .$status. '" onload="killToats()"';
$resultat .= '<div class="toats_headers">';
$resultat .= '<a class="toats_die">X</a>';
$resultat .= '<h5><i class="fas fa-exclamation-circle"></i> Notification :</h5>';
$resultat .= '<div class="toats_core">';
$resultat .= '<p>' .$msg. '</p>';
$resultat .= '</div>';
$resultat .= '</div>';
$tableau['resultat'] = $resultat;
$tableau['status'] = $status;
echo json_encode($tableau);
?>
le script ajax doit :
-si error, juste renvoyé une notif d'erreur
si success, envoyé une notification de success + fermer le modal
J'ai bien la notification quand il y a une erreur, mais aucune notification lors d'un success, pourtant l'inserstion en BDD fonctionne sans problemes