Bonjour,
J'ai 3 listes déroulantes liées en ajax (Région -> Départ -> Commune) créer suivant cette méthode : http://siddh.developpez.com/articles/ajax/#LIV-A
Je n'arrive pas garder la valeur du 2 ème et 3 ème select après envoi du formulaire, pouvez vous m'aider.
Voici mon code simplifier pour aller droit au but :
Fichier qui contient le formulaire (post.php)
php du deuxième select (departement.php)
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
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 <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 { alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); xhr = false; } return xhr; } function depart(){ var xhr = getXhr(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ leselect = xhr.responseText; document.getElementById('divcity').innerHTML = leselect; } } xhr.open("POST","lieu/departement.php",true); xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); sel = document.getElementById('countryid'); countryid = sel.options[sel.selectedIndex].value; xhr.send("countryid="+countryid); } function ville(){ var xhr = getXhr(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ leselect = xhr.responseText; document.getElementById('divarea').innerHTML = leselect; } } xhr.open("POST","lieu/ville.php",true); xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); sel = document.getElementById('cityid'); cityid = sel.options[sel.selectedIndex].value; xhr.send("cityid="+cityid); } </script> <form action="post.php" method="post" name="frmPost" enctype="multipart/form-data"> <div class="labelform" style="clear: left;"> <label for="region">Région</label> </div> <div class="adinput"> <select name='countryid' id='countryid' onchange='depart()'> <option value=''> «Choisissez la région» </option> <? $res = mysql_query("SELECT countryid, countryname FROM $t_countries ct ORDER BY countryname"); while($row=mysql_fetch_array($res)) { echo "<option value=\"$row[countryid]\""; if ($row['countryid'] == $_REQUEST['countryid']) echo " selected"; echo "> $row[countryname]</option>"; } ?> </select> </div> <div class="labelform" style="clear: left;"> <label for="Département">Département</label> </div> <div id='divcity' style='display:inline'> <select name='cityid'> <option value="-1">«Choisissez le département»</option> </select> </div> <div class="labelform" style="clear: left;"> <label for="Commune">Commune</label> </div> <div id='divarea' style='display:inline'> <select name='area'> <option value="" selected="selected">«Choisissez la commune»</option> </select> <?php if($err_area) echo "<div style=\"display: block;\"><span class=\"error\">$err_area</span></div>"; ?> </div> </form>
php du troisième select (ville.php)
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 <?php if(isset($_POST["countryid"])){ $sql = ("SELECT cityid, cityname FROM ".$t_cities." WHERE countryid = ".$_POST["countryid"]." ORDER BY cityname"); $res = mysql_query($sql); if (mysql_num_rows($res)) { ?> <select name='cityid' id='cityid' onchange='ville()'> <option value="-1" > «Choisissez le département»</option> <?php $other_index = 1; while ($row = mysql_fetch_array($res)) { $other_index++; if($_REQUEST['cityid']==$row['cityid']) { $selectchoix=' selected="selected"'; } else { $selectchoix=''; } echo '<option value="'.$row['cityid'].'"'.$selectchoix.'>'.$row['cityname'].'</option>'; //echo '<option value="'.$row['cityid'].'" '.(($data['cityid']==$row['cityid'])?'selected="selected"':'').'>'.$row['cityname'].'</option>'; } ?> </select> <?php } } ?>
Merci pour votre aide
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 <?php if(isset($_POST["cityid"])){ $sql = ("SELECT areaname FROM $t_areas WHERE cityid = ".$_POST["cityid"]." ORDER BY areaname"); $res = mysql_query($sql); if (mysql_num_rows($res)) { ?> <select name="area"> <option value="" selected="selected"> «Choisissez la commune» </option> <?php $other_index = 1; while ($row = mysql_fetch_array($res)) { $other_index++; echo '<option value="'.$row[areaname].'"'; if ($data['area'] == $row['areaname']) echo ' selected="selected"'; echo '>'.$row[areaname].'</option>'; } ?> </select> <?php } } ?>
Partager