IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AJAX Discussion :

Passage de 2 variables dans requête ajax


Sujet :

AJAX

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Inscrit en
    Janvier 2008
    Messages
    1 159
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 1 159
    Par défaut Passage de 2 variables dans requête ajax
    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
        }
    }
    ?>

  2. #2
    Membre Expert

    Homme Profil pro
    développeur
    Inscrit en
    Octobre 2013
    Messages
    1 583
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : développeur

    Informations forums :
    Inscription : Octobre 2013
    Messages : 1 583
    Par défaut
    Salut,

    Test dans phpmyadmin ou adminer ta requête sql
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    SELECT nom_service_public FROM ef_servicepublic WHERE commune = :sid and code_postal = :code_postal ORDER BY nom_service_public
    fait un var_dump de $commune et $code_postal (utilise plutôt le camel case codePostal ) et ajoute les pour le teste avec ta requête. Fait également un var_dump de $results.

  3. #3
    Membre éprouvé
    Inscrit en
    Janvier 2008
    Messages
    1 159
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 1 159
    Par défaut
    je fais ca de suite si tu peux patienter 5 min je dois finir ca rapidement . merci beaucoup.

    les vardump me renvoie
    Code postal:
    Ville :
    string(26) "[object HTMLSelectElement]" string(26) "[object HTMLSelectElement]" array(0) { }
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
        $sql = "SELECT nom_service_public FROM ef_servicepublic WHERE commune = :sid and code_postal = :code_postal ORDER BY nom_service_public";
       var_dump ($commune);
         var_dump ($code_postal);
    	try {
            $stmt = $DB->prepare($sql);
            $stmt->bindValue(":sid", trim($commune));
    		$stmt->bindValue(":code_postal", trim($code_postal));
            $stmt->execute();
            $results = $stmt->fetchAll();
     
    	var_dump ($results);

    j'ai bien un résultat retourner pour
    Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
    SELECT * FROM ef_servicepublic WHERE commune = "BOURG-EN-BRESSE CEDEX" AND code_postal="01005"

    dans le phpmyadmin et quand je remplace les valeurs des variables au direct dans le fichier php.

  4. #4
    Membre Expert

    Homme Profil pro
    développeur
    Inscrit en
    Octobre 2013
    Messages
    1 583
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : développeur

    Informations forums :
    Inscription : Octobre 2013
    Messages : 1 583
    Par défaut
    Dans ton code enlève les trim, cela enlève les espaces BOURG-EN-BRESSE CEDEX -> BOURG-EN-BRESSECEDEX

  5. #5
    Membre éprouvé
    Inscrit en
    Janvier 2008
    Messages
    1 159
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 1 159
    Par défaut
    j'ai modifier merci en tout cas de m'aider.

    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
    require("configure.php");
    $commune = ($_REQUEST["commune"] <> "") ? ($_REQUEST["commune"]) : "";
    $code_postal = ($_REQUEST["code_postal"] <> "") ? ($_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", ($commune));
    		$stmt->bindValue(":code_postal", ($code_postal));
            $stmt->execute();
            $results = $stmt->fetchAll();
     
     
     
        } catch (Exception $ex) {
            echo($ex->getMessage());
        }


    ca peut pas etre l'affichage car avec les données direct la derniere select s'affiche plus la requete je pense.

    message toujours pas de service inscrit donc il trouve 0 resultat alors que en direct ca fonctionne.

    comment je pourrai verifier les variable car var dump ne marche pas ni echo et alert.

  6. #6
    Membre Expert

    Homme Profil pro
    développeur
    Inscrit en
    Octobre 2013
    Messages
    1 583
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : développeur

    Informations forums :
    Inscription : Octobre 2013
    Messages : 1 583
    Par défaut
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $stmt->bindValue(":sid", ($commune));
    		$stmt->bindValue(":code_postal", ($code_postal));

    Je ne sais pas si ça a un impact mais plutôt
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $stmt->bindValue(":sid", $commune);
    		$stmt->bindValue(":code_postal", $code_postal);

    le var_dump de results est vide? fait un echo de test variables et un var_dump de results.
    Cela est peut-être du au type si tu fais un cast?
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $stmt->bindValue(":sid", (string)$commune);
    		$stmt->bindValue(":code_postal", (string)$code_postal);

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Cookies] Variable arrondie mais pas dans le cookie
    Par afrodje dans le forum Langage
    Réponses: 7
    Dernier message: 17/04/2008, 12h12
  2. pourquoi je ne passe pas dans mon action
    Par fk04 dans le forum Struts 1
    Réponses: 5
    Dernier message: 03/10/2007, 21h20
  3. Ctrl -] ne passe pas dans une console avec vim
    Par Celelibi dans le forum Applications et environnements graphiques
    Réponses: 4
    Dernier message: 10/03/2006, 13h35
  4. Réponses: 2
    Dernier message: 07/03/2006, 01h53
  5. Réponses: 8
    Dernier message: 26/10/2005, 03h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo