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
| <!-- nouvetu.inc -->
<html>
<head>
<script type='text/javascript'>
function getXhr(){
var xhr = null;
if(window.XMLHttpRequest){ // Firefox et autres
xhr = new XMLHttpRequest();
}
else if(window.ActiveXObject){ // Internet Explorer
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else { // XMLHttpRequest non supporté par le navigateur
alert("Votre navigateur ne supporte XMLHTTPRequest...");
xhr = false;
}
return xhr;
}
/**
* Méthode qui sera appelée sur le click du bouton
*/
function change(){
var xhr = getXhr();
// On défini ce qu'on va faire quand on aura la réponse
xhr.onreadystatechange = function() {
//alert(xhr.readyState);
// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
//alert(xhr.readystate);
//alert(xhr.status);
if(xhr.readyState == 4 && xhr.status == 200){
di = document.getElementById('cont');
di.innerHTML = xhr.responseText;
alert(xhr.responseText);
}
}
// Ici on va voir comment faire du post
xhr.open("POST","includes/ajaxcont.php",true);
// ne pas oublier ça pour le post
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
// ne pas oublier de poster les arguments
identreprise = document.getElementById('entre').options[document.getElementById('entre').selectedIndex].value;
//alert(identreprise);
xhr.send("num_entr="+identreprise);
}
</script>
</head>
<body>
<center><font color = "navy" size = "5">Ajout d'un nouvel apprenti</font></center>
<form method="POST" action="index.php?lien=ajout">
<table>
[...]
<td>Entreprise :</td>
<td><select name='entre' id='entre' onchange='change()'>
<option value='-1'>Choisisez une entreprise</option>
<?
mysql_select_db("lyceecon0");
$res = mysql_query("SELECT num_entr,nom_entre FROM entreprise ORDER BY nom_entre");
while($row = mysql_fetch_assoc($res))
{
echo "<option value='".$row["num_entr"]."'>".$row["nom_entre"]."</option>";
}
?>
</select></td>
<td></td>
<td>Contact :</td>
<div id='cont' style='display:inline'>
<td><select name='cont' id = 'cont'>
<option value='-1'>Choisisez un Contact</option>
</select></td>
</div>
</tr>
</table>
et l'autre fichier
<!-- ajaxcont.php-->
<?php
mysql_connect("localhost","root","");
mysql_select_db("lyceecon0");
echo '<select name="cont" id = "cont">';
if(isset($_POST["num_entr"]))
{
$res = mysql_query("SELECT num_pers,nom_pers FROM personnel WHERE num_entr=".$_POST["num_entr"]." ORDER BY nom_pers");
while($row = mysql_fetch_assoc($res))
{
if ($row)
{
echo '<option value="'.$row["num_pers"].'">'.$row["nom_pers"].'</option>';
}
}
}
else
{
echo "<option value='-1'>Choisir un contact</option>";
echo "</select>";
}
?> |
Partager