Bonjour,
J'ai un formulaire avec différents champs et donc certain dépendent d'une liste. Je fait un controle en php sur la date (requête sql) et si il y a une erreur, je retourne le formulaire avec les champs rempli et là problème: Je n'arrive pas à remplir les champs qui dépendent de la liste sauf un: champ6, les autres qui dépendent de d'autres valeur de la liste, je n'arrive pas à les remplir.
Voici la fonction qui permet d'afficher les champs en fonction de la valeur de la liste:
Code JS : 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 //Fonction qui permet de cacher des champs et de les rendres visible selon la sélection $(document).ready(function() { $('#champ1').hide(); $('#champ2').hide(); $('#champ3').hide(); $('#champ4').hide(); $('#champ5').hide(); $('#champ6').hide(); $('select[name="type"]').change(function() { // lorsqu'on change de valeur dans la liste var valeur = $(this).val(); // valeur sélectionnée var champ1=$("#champ1").val(); var champ2=$("#champ2").val(); var champ3=$('select[name="champ3"]').val(); var champ4=$("#champ4").val(); var champ5=$("#champ5").val(); var champ6=$("#champ6").val(); if(valeur != '') { // si non vide if(valeur == 'val1' || valeur == 'val2' || valeur == 'val3') { //Affichage des différents champs $('#div1').show(); $('#div2').show(); $('#div3').show(); $('#div4').show(); $('#champ5').val(''); $('#champ6').val(''); $("#champ1").val(champ1); $('select[name="champ2"]').val(champ2); $("#champ3").val(champ3); $("#champ4").val(champ4); } else { //On cache les différents champs si autre valeur sélectionné $('#div1').hide(); $('#div2').hide(); $('#div3').hide(); $('#div4').hide(); $('#champ5').val(''); $('#champ6').val(''); } if(valeur == 'val3') { $('#div5').show(); $('#champ6').val(''); $("#champ5").val(champ5); } else{ $('#div5').hide(); $('#champ6').val(''); } if(valeur == 'val4' || valeur == val5' || valeur == 'val6') { $('#div6').show(); $('#champ1').val(''); $('#champ2').val(-1); $('#champ3').text(''); $('#champ4').text(''); $('#champ5').val(''); $("#champ6").val(champ6); } else{ $('#div6').hide(); $('#champ1').val(''); $('#champ2').val(-1); $('#champ3').text(''); $('#champ4').text(''); $('#champ5').val(''); } } }).trigger('change'); });
Formulaire:
Code HTML : 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
99
100
101
102
103 <form class="new_user_form" role="form" id="new" method="post"> <div class="col-md-12 field-box"> <label for="date">Date :<span class="help-required"></span></label> <div class="col-md-9"> <input type="text" id="date" name="date" value="<?php echo $date ?>" class="form-control required" aria-required="true" /> </div> </div> <div class="col-md-12 field-box"> <label for="lieu">Lieu:<span class="help-required"></span></label> <div class="col-md-9"> <input class="form-control required" type="text" name="lieu" id="lieu" value="<?php echo $lieu ?>" role="input" aria-required="true" /> </div> </div> <div class="col-md-12 field-box"> <label for="type">Type:<span class="help-required"></span></label> <div class="col-md-9 col-md-push-1"> <select style="width:250px" class="select2 type required" id="type" name="type" aria-required="true"> <option></option> <?php //Appel de la fonction pour récupérer les valeur possible d'un champ type enum $enumFields = getEnumFieldValues('detail', 'type', $connexion); foreach($enumFields as $value): $value=str_replace("'","",$value); if($value == $type): ?> <option value="<?php echo $value ?>" selected="selected"><?php echo $value; ?></option> <?php else: ?> <option value="<?php echo $value ?>"><?php echo $value ?></option> <?php endif; endforeach; ?> </select> </div> </div> <div class="col-md-12 field-box" id="div5" name="div5"> <label for="div5">champ5:<span class="help-required"></span></label> <div class="col-md-9"> <input class="form-control" type="text" name="champ5" id="champ5" value="<?php echo $champ5?>" role="input"/> </div> </div> <div class="col-md-12 field-box" id="div1" name="div1"> <label for="div1">champ1 :<span class="help-required"></span></label> <div class="col-md-9"> <input class="form-control" type="text" name="champ1 " id="champ1 " value="<?php echo $champ1 ?>" role="input" onkeyup="calcul(); replace();" /> </div> </div> <div class="col-md-12 field-box" id="div2" name="div2"> <label for="div2">champ2:<span class="help-required"></span></label> <div class="col-md-9 col-md-push-1"> <select style="width:250px" class="form-control" id="champ2" name="champ2" onchange="calcul();"> <?php $req2 = "Select id AS id, tx From champ Order by tx"; //Exécution de la requête $res2 = $connexion->query($req2); //Tant qu'il y a des enregistrements, on les affiche while($row2 = $res2->fetch()): if($row2['id'] == $champ2): ?> <option value="<?php echo $row2['id'] ?>" selected="selected"><?php echo $row2['tx']; ?></option> <?php else: ?> <option value="<?php echo $row2['id']?>"><?php echo $row2['tx']; ?></option> <?php endif; endwhile; ?> </select> </div> </div> <div class="col-md-12 field-box" id="div3" name="div3"> <label for="div3">champ3 :</label> <div class="col-md-9"> <label type="text" name="champ3" id="champ3 " value="<?php echo $champ3?>"></label> </div> </div> <div class="col-md-12 field-box" id="div4" name="div4"> <label for="div4">champ4 :</label> <div class="col-md-9"> <label type="text" name="champ4" id="champ4 " value="<?php echo $champ4?>"></label> </div> </div> <div class="col-md-12 field-box" id="div6" name="div6"> <label for="div6">champ6:<span class="help-required"></span></label> <div class="col-md-9"> <input class="form-control" type="text" name="champ6 " id="champ6" value="<?php echo $champ6?>" role="input" onkeyup="replace();"/> </div> </div> <div class="col-md-11 field-box actions"> <input type="submit" value="Ajouter" name="submit" id="submitButton" class="btn-glow primary" title="Cliquez ici pour ajouter!" /> </div> </form>
Merci de votre aide
Partager