je débute en JavaScript, je désire utiliser la méthode POST avec AJAX mais je n'y parviens pas. Je ne comprends pas le fonctionnement. Mon bute est d'alimenter un select de façon dynamique. pour le moment afin de bien comprendre le principe je veux juste récupérer le résultat dans un div.
Merci de votre aide.
voici mes code :
Formulaire:
Code : Tout sélectionner
<?php
try
{
$db = new PDO('mysql:host=localhost;dbname=cs_semur;charset=utf8', 'root', 'root');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$distinc_matricule =
"SELECT DISTINCT t_inscription_formation_copie.matricule , t_personnel.nom, t_personnel.prenom
FROM t_inscription_formation_copie
JOIN t_personnel on t_inscription_formation_copie.matricule = t_personnel.matricule ";
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<!-- En-tête de la page -->
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="">
<title>Formulaire essais</title>
</head>
<body>
<form action = "" method="post">
<h1 class="selection_personnel"> SELECTION DU PERSONNEL </h1>
<div class = "p_select_personnel"><label for ="personnel"> Nom: </label>
<select id="personnel" onchange ="getchange();" class="personnel" name ="personnel">
<option value="" selected> </option>
<?php
$requete = $db -> prepare($distinc_matricule);
$requete -> execute();
$resultat = $requete -> fetchAll();
foreach ($resultat as $ligne)
{
?>
<option value = "<?php echo $ligne ['matricule'];?>">
<?php echo $ligne['matricule']," ",$ligne['nom']," ",$ligne['prenom']; ?>
</option>
<?php
}
?>
</select>
</div>
<div id = "demo">
</div>
</form>
<script type="text/javascript" src="index_essais.js"></script>
</body>
</html>
Code : Tout sélectionner
<?php
try
{
$db = new PDO('mysql:host=localhost;dbname=cs_semur;charset=utf8', 'root', 'root');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$matricule = $_POST['matricule'];
if(isset($_POST['matricule'])){
$requete = $db -> prepare("SELECT * FROM t_inscription_formation_copie WHERE matricule = ?");
$requete -> execute();
$resultat = $requete -> fetchAll();
foreach ($resultat as $ligne){
echo $ligne['id_inscription_formation'];
}
}
?>
Code : Tout sélectionner
function getchange(){
var xhr = new XMLHttpRequest();
var champ_matricule = document.getElementById("personnel").value;
var demo = document.getElementById("demo");
var data = "matricule="+champ_matricule;
xhr.onreadystatechange = function () {
console.log (this);
if(xhr.readyState == 4 && xhr.status == 200){
demo.innerHTML = xhr.response;
}
};
xhr.open("POST","index_essais.php",true);
xhr.responseType = "text";
xhr.send(data);
};