Dans un formulaire j'ai une liste déroulante qui permet de choisir entre Départemental ou Régional et selon ce choix la liste des régions ou des départements apparait dans une autre liste déroulante.
Pour cela j'utilise jQuery qui appelle grâce à de l'ajax une fonction php qui liste les départements ou les régions et qui les retourne en xml à la fonction ajax qui créer ensuite la nouvelle liste. Sous Firefox et Chrome cela marche mais pas sous IE, quelqu'un aurait une idée?
Merci
Voici la fonction javascript :
Voici la fonction qui génère le xml :
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 /* * Fonction pour afficher la liste des départements * ou des régions */ $(function localisation(){ $("#localisation").change(function(){ $('#listeDep').remove(); $('#listeReg').remove(); $.ajax({ type: "GET", url: "modeles/localisation.php", data: {localisation:$("#localisation").val()}, success: function(xml){ if($("#localisation").val()==0) { $("#localisation").after($('<select/>').attr("id","listeDep").attr("name","listeDep")); $('departement',xml).each(function(){ $('#listeDep').append($('<option />').attr("value",$(this).find('numeroDepartement').text()) .attr("id",$(this).find('numeroDepartement').text()) .text($(this).find('nom').text()+" ("+$(this).find('numeroDepartement').text()+")")); }); } if($("#localisation").val()==1) { $("#localisation").after($('<select/>').attr("id","listeReg").attr("name","listeReg")); $('region',xml).each(function(){ $('#listeReg').append($('<option />').attr("value",$(this).find('idRegion').text()) .attr("id",$(this).find('idRegion').text()) .text($(this).find('nom').text())); }); } } }); }); });
Et voici le code HTML concerné :
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 <?php /* * Script PHP appelé par une fonction AJAX de jQuery lors * de la consultation de la liste des demandes pour filtrer * par localisation */ include_once('mConsultationDemande.php'); $tab=mConsultationDemande::localisation($_GET['localisation']); if($_GET['localisation']==0){ $xml="<departements>"; foreach($tab as $value) { $xml.="<departement>"; $xml.="<numeroDepartement>".$value['numeroDepartement']."</numeroDepartement>"; $xml.="<nom>".$value['nom']."</nom>"; $xml.="</departement>"; } $xml.="</departements>"; } else { $xml="<regions>"; foreach($tab as $value) { $xml.="<region>"; $xml.="<idRegion>".$value['idRegion']."</idRegion>"; $xml.="<nom>".$value['nom']."</nom>"; $xml.="</region>"; } $xml.="</regions>"; } echo($xml); ?>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 echo("<div>"); echo("<label class='form_label' for='localisation'>Par localisation</label>"); echo("<select id='localisation' name='localisation'>"); echo("<option value=''>Choisissez une localisation</option>"); echo("<option value='0'>Départementale</option>"); echo("<option value='1'>Régionale</option>"); echo("</select>"); echo("</div>");
Partager