Bonjour,

j'ai 3 liste lié qui fonctionne seul la derniere liste doit avoir 2 variable en passsage j'ai modifie donc la fonction mais il me retourne pas de resultat mon message Pas de service public.

Je souhaite recuperer la valeur de la liste 1 et liste 2 pour faire la requete dans fetch_city pour la liste 3.

index :
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="icon" href="http://www.thesoftwareguy.in/favicon.ico" type="image/x-icon" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Multiple dropdown with jquery ajax and php">
        <meta name="keywords" content="Multiple dropdown with jquery ajax and php">
        <meta name="author" content="Shahrukh Khan">
        <title>Multiple dropdown with jquery ajax and php - thesoftwareguy</title>
        <link rel="stylesheet" href="style.css" type="text/css" />
        <style>
            select {
                padding:3px;
                border-radius:5px;
                background: #f8f8f8;
                color:#000;
                border:1px solid #EB028F;
                outline:none;
                display: inline-block;
                width:250px;
                cursor:pointer;
                text-align:left;
                font:inherit;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <div id="body">
                <div class="mainTitle" >Multiple dropdown with jquery ajax and php</div>
                <div class="height20"></div>
                <article>
                    <table style="margin:0 auto;width:50%" >
                        <tr>
                            <td align="center" height="50">
                                <?php
                                $sql = "SELECT distinct(code_postal) FROM ef_servicepublic ORDER BY code_postal";
                                try {
                                    $stmt = $DB->prepare($sql);
                                    $stmt->execute();
                                    $results = $stmt->fetchAll();
                                } catch (Exception $ex) {
                                    echo($ex->getMessage());
                                }
                                ?>
                                <label>Code postal:
                                    <select name="code_postal" id="code_postal" onChange="showState(this);">
                                        <option value="">Please Select</option>
                                        <?php foreach ($results as $rs) { ?>
                                            <option value="<?php echo $rs["code_postal"]; ?>"><?php echo $rs["code_postal"]; ?></option>
                                        <?php } ?>
                                    </select>
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" height="50"><div id="output1"></div> </td>
                        </tr>
                        <tr>
                            <td align="center" height="50"><div id="output2"></div> </td>
                        </tr>
                    </table> 
 
 
 
                </article>
 
            </div>
        </div>
        <script src="jquery-1.9.0.min.js"></script>
        <script>
                    function showState(sel) {
                        var code_postal = sel.options[sel.selectedIndex].value;
                        $("#output1").html("");
                        $("#output2").html("");
                        if (code_postal.length > 0) {
 
                            $.ajax({
                                type: "POST",
                                url: "fetch_state.php",
                                data: "code_postal=" + code_postal,
                                cache: false,
                                beforeSend: function() {
                                    $('#output1').html('<img src="loader.gif" alt="" width="24" height="24">');
                                },
                                success: function(html) {
                                    $("#output1").html(html);
                                }
                            });
                        }
                    }
 
                    function showCity() {
                                
                        //var commune = sel.options[sel.selectedIndex].value;
                                                
var code_postal = document.getElementById("code_postal" );
var commune = document.getElementById("commune" );
typeof(document.getElementById(commune));
if ((code_postal.options[code_postal.options.selectedIndex].value)
    && (commune.options[commune.options.selectedIndex].value ))
{
  
                        if (commune.length > 0) {
                            $.ajax({
                                type: "POST",
                                url: "fetch_city.php",
                                data: "commune=" + commune +'&code_postal='+ code_postal,
                                cache: false,
                                beforeSend: function() {
                                    $('#output2').html('<img src="loader.gif" alt="" width="24" height="24">');
                                },
                                success: function(html) {
                                    $("#output2").html(html);
                                }
                            });
                        } else {
                            $("#output2").html("");
                        }
                    }
                                        
                                        }      
 
                                        </script>

le script php fetch_state.php :

Code php : 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
require("configure.php");
$commune = ($_REQUEST["commune"] <> "") ? trim($_REQUEST["commune"]) : "";
$code_postal = ($_REQUEST["code_postal"] <> "") ? trim($_REQUEST["code_postal"]) : "";
 
 
if ($commune <> "") {
 
    $sql = "SELECT nom_service_public FROM ef_servicepublic WHERE commune = :sid and code_postal = :code_postal ORDER BY nom_service_public";
 
	try {
        $stmt = $DB->prepare($sql);
        $stmt->bindValue(":sid", trim($commune));
		$stmt->bindValue(":code_postal", trim($code_postal));
        $stmt->execute();
        $results = $stmt->fetchAll();
 
 
 
    } catch (Exception $ex) {
        echo($ex->getMessage());
    }
 
 
     if (count($results) > 0) {
        ?>
        <label>Nom service public: 
            <select name="city" id="city" name="box">
                <option value="">Please Select</option>
                <?php foreach ($results as $rs) { ?>
                    <option value="<?php echo $rs["id_service_public"]; ?>"><?php echo $rs["nom_service_public"]; ?></option>
                <?php } ?>
            </select>
        </label>
        <?php
    }
	else
	{
		echo'<label>Pas de service public</label>';
	}
}

le fetch_city.php :

Code php : 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
<?php
/*
 * @author Shahrukh Khan
 * @website <a href="http://www.thesoftwareguy.in" target="_blank">http://www.thesoftwareguy.in</a>
 * @facebbok <a href="https://www.facebook.com/Thesoftwareguy7" target="_blank">https://www.facebook.com/Thesoftwareguy7</a>
 * @twitter <a href="https://twitter.com/thesoftwareguy7" target="_blank">https://twitter.com/thesoftwareguy7</a>
 * @googleplus <a href="https://plus.google.com/+thesoftwareguyIn" target="_blank">https://plus.google.com/+thesoftwareguyIn</a>
 */
 
 
require("configure.php");
$code_postal = ($_REQUEST["code_postal"] <> "") ? trim($_REQUEST["code_postal"]) : "";
if ($code_postal <> "") {
    $sql = "SELECT distinct(commune) FROM ef_servicepublic WHERE code_postal = :cid ORDER BY commune";
    try {
        $stmt = $DB->prepare($sql);
        $stmt->bindValue(":cid", trim($code_postal));
        $stmt->execute();
        $results = $stmt->fetchAll();
    } catch (Exception $ex) {
        echo($ex->getMessage());
    }
    if (count($results) > 0) {
        ?>
        <label>Ville : 
            <select name="commune" id="commune" onchange="showCity(this);">
                <option value="">Please Select</option>
                <?php foreach ($results as $rs) { ?>
                    <option value="<?php echo $rs["commune"];$code_postal ?>"><?php echo $rs["commune"]; ?>  (<?php echo $code_postal ?>)</option>
                <?php } ?>
            </select>
        </label>
        <?php
    }
}
?>