2016-09-12 218 views
0

我有一个下拉列表,我去DATABSE往里面:选择的项目 - 下拉列表 - SQL

<TH> 
    <FORM> 
     <p>Département</p> 
     <SELECT size="1" id="depart" > 
      <OPTION> 
      <?php 
       try { 
        // Parametres connexion 
        $bdd = new PDO('mysql:host=localhost;dbname=db', 'root', '') or die ("Impossible de se connecter au serveur où est stocké la Base de Données."); 
        // Requête 
        $resultats = $bdd -> query("SELECT DISTINCT Departement                   FROM adresse                     ORDER BY Departement ASC"); 
        // Tant qu'il y a des enregistrements, remplir la liste déroulante 
        while($d = $resultats->fetch()) 
        { 
         echo '<option value="'.$d["Departement"].'">'.$d["Departement"].'</option><br/>'; 
        } 
       } 
       catch(PDOException $e){ 
        echo 'Erreur : ' . $e->getMessage(); 
       } 
      ?> 
      </OPTION> 
     </SELECT> 

     <!-- jQuery : Récupère le departement choisi --> 
     <script> 
      var departement_ = ''; 
      $('#departement').change(function departement() {departement_ = $('#departement option:selected').first().attr('value'); 

      // Display on input named "pu" 
      // $('#pu').val(depart_); 

      }); 
     </script> 
    </FORM> 
</TH> 

正如你所看到的,我检索在下拉列表中选择的项目:

<TH> 
    <FORM> 
    <p>Commune</p> 
    <SELECT size="1" id="commune" > 
     <OPTION> 
     <?php 
      try { 
       // Parametres connexion 
       $bdd = new PDO('mysql:host=localhost;dbname=db', 'root', '') or die ("Impossible de se connecter au serveur où est stocké la Base de Données."); 
       // Requête 
       $resultats = $bdd -> query("SELECT DISTINCT Commune                     FROM adresse                     WHERE Departement='AVEYRON'                      ORDER BY Commune ASC"); 
       // Tant qu'il y a des enregistrements, remplir la liste déroulante 
       while($d = $resultats->fetch()) 
       { 
        echo '<option value="'.$d["Commune"].'">'.$d["Commune"].'</option><br/>'; 
       } 
      } 
      catch(PDOException $e){ 
       echo 'Erreur : ' . $e->getMessage(); 
      } 
      ?> 
     </OPTION> 
    </SELECT> 

    <!-- jQuery : Récupère le code postal choisi --> 
    <script> 
    var commune_ = ''; 
    $('#commune').change(function commune() { 
      commune_ = $('#commune option:selected').first().attr('value'); 

      // Display on input named "pu" 
      // $('#pu').val(commune_); 

      }); 
    </script> 
</FORM> 

正如您所看到的,我再次检索所选的项目。

让我继续。第一个列表是在这里为用户选择一个部门。为此,我执行一个SQL请求。然后,我检索选择的项目。我把它放在jQuery的var(看我的代码)。我希望填写第一个下拉列表中选定项目的第二个下拉列表,因为每个部门的“Code Postal(法国)”(或英文ZipCode)列表都将更改。

你能帮助我吗?

+0

什么问题?你不能填充第一个组合框?你不能得到选定的项目?你不能填充第二个组合框? – Natrium

+0

我想执行不同的SQL请求,但我不知道如何选择该项目。 – McNavy

+0

您正在混合您的服务器端和客户端技术。你不能直接从jquery运行SQL - 你需要做一个Ajax调用。 –

回答

0

您可以在解决方案波动中不使用ajax,但您必须同时执行两个查询,并在javascript中执行您的逻辑,而不是每次在选择列表中进行更改时都执行新的sql查询。

如果您有大量的结果,那么最好使用ajax,而不是一次加载所有数据。

为您的代码的其余几条建议: *不要使用大写的HTML标签 *不要在一个文件中 多次到同一个mysql数据库连接*尽量分开PHP,JavaScript和HTML/CSS代码

<?php 
try { 
    // Parametres connexion 
    $bdd = new PDO('mysql:host=localhost;dbname=db', 'root', '') 
    or die ("Impossible de se connecter au serveur où est stocké la Base de Données."); 
    // Requête 
    $resultats = $bdd->query("SELECT DISTINCT Commune,Departement FROM adresse ORDER BY Commune ASC"); 
    // Tant qu'il y a des enregistrements, remplir la liste déroulante 
    $select_commune = ''; 
    while ($d = $resultats->fetch()) { 
     $select_commune .= '<option data-department="' . $d["Departement"] . '" value="' . $d["Commune"] . '">' . $d["Commune"] . '</option>'; 
    } 

    $resultats = $bdd->query("SELECT DISTINCT Departement FROM adresse ORDER BY Departement ASC"); 
    // Tant qu'il y a des enregistrements, remplir la liste déroulante 
    $select_depart = ''; 
    while ($d = $resultats->fetch()) { 
     $select_depart .= '<option value="' . $d["Departement"] . '">' . $d["Departement"] . '</option><br/>'; 
    } 
} catch (PDOException $e) { 
    echo 'Erreur : ' . $e->getMessage(); 
} 
?> 
<th> 
    <p>Département</p> 
    <select size="1" id="departement"> 
     <option value="">Choose department</option> 
     <?= $select_depart ?> 
    </select> 
</th> 
<th> 
    <p>Commune</p> 
    <select size="1" id="commune"> 
     <option value="">Choose Commune</option> 
     <?= $select_commune ?> 
    </select> 
</th> 

<script> 
    $(function() { 
     $('#commune option').hide(); 
     var departement_ = ''; 
     $('#departement').change(function departement() { 
      $('#commune option').hide(); 
      $('#commune option').first().show(); 
      departement_ = $('#departement option:selected').first().attr('value'); 
      $('#commune option[data-department="' + departement_ + '"]').show(); 
      // Display on input named "pu" 
      // $('#pu').val(depart_); 
     }); 
     var commune_ = ''; 
     $('#commune').change(function commune() { 
      commune_ = $('#commune option:selected').first().attr('value'); 

      // Display on input named "pu" 
      // $('#pu').val(commune_); 
     }); 
    }); 
</script> 
相关问题