Les Snippets

Connexion

Listes déroulantes liées

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 30/03/2006 09:44:08 et initié par malalam [Liste]
Date de mise à jour : 03/04/2006 11:03:05
Vue : 30649
Catégorie(s) : WinForm
Langage sélectionné : PHP 5
Langages dispo pour ce code :
- PHP 3, PHP 4, PHP 5
- Javascript
- Voir tous les langages pour ce code snippet



Langage : PHP 3 , PHP 4 , PHP 5
Date ajout : 30/03/2006
Posté par malalam [Liste]
DateMAJ : 30/03/2006

/**
* version utilisant une requête xmlhttp
*/
<script type="text/javascript">
if (window.XMLHttpRequest) {
   oXmlhttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
   oXmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

function search (clef) {
 oXmlhttp.open('POST','<?php echo $_SERVER['PHP_SELF']; ?>');
 oXmlhttp.onreadystatechange=function() {
  if (oXmlhttp.readyState==4 && oXmlhttp.status == 200) {
   document.body.innerHTML = oXmlhttp.responseText;
  }
 }
 oXmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 var data = 'liste1='+clef
 oXmlhttp.send (data);
}

</script>
<?php
$aTab = array (1 => array ('test', 'pour', 'une', 'blonde'),
  2 => array ('difficile', 'ca?', 'non...'),
  3 => array ('très', 'facile', 'en', 'fait!'));
?>

<form method="post">
<select name="liste1" onchange="search (this.value);">
<?php
foreach ($aTab as $clef => $dump) {
 $selected=(isset($_POST['liste1']) && $_POST['liste1'] == $clef)?'selected="selected"':'';
 echo '<option value="',$clef,'" ',$selected,'>',$clef,'</option>';
}
?>
</select>
<?php
if (isset ($_POST['liste1']) && !empty ($_POST['liste1'])) {
 echo '<select name="liste2">';
 foreach ($aTab[$_POST['liste1']] as $val) {
  echo '<option value="',$val,'">',$val,'</option>';
 }
 echo '</select>';
}
?>
</form>


/**
* version n'utilisant pas de requête xmlhttp
*/
<?php
$aTab = array (1 => array ('test', 'pour', 'une', 'blonde'),
  2 => array ('difficile', 'ca?', 'non...'),
  3 => array ('très', 'facile', 'en', 'fait!'));
?>

<form method="post" id="mainForm">
<select name="liste1" onchange="document.getElementById('mainForm').submit();">
<?php
foreach ($aTab as $clef => $dump) {
 $selected=(isset($_POST['liste1']) && $_POST['liste1'] == $clef)?'selected="selected"':'';
 echo '<option value="',$clef,'" ',$selected,'>',$clef,'</option>';
}
?>
</select>
<?php
if (isset ($_POST['liste1']) && !empty ($_POST['liste1'])) {
 echo '<select name="liste2">';
 foreach ($aTab[$_POST['liste1']] as $val) {
  echo '<option value="',$val,'">',$val,'</option>';
 }
 echo '</select>';
}
?>
</form>

Remarque :
Le choix effectuté dans la 1ère liste conditionne la génération de la seconde liste.

Snippets en rapport avec : Liées, Ajax, Xmlhttp, Liste, Déroulante



Codes sources en rapport avec : Liées, Ajax, Xmlhttp, Liste, Déroulante

{PHP} CHAINER 3 LISTES DÉROULANTES EN AJAHT
Voici un exemple pour chainer 3 listes déroulantes en AJAHT (HTML ou Text) un pseudo AJAX sans XML. ...

{C# / C#.NET} TOOLTIP TEXT POUR LA LISTE DÉROULANTE D'UN COMBOBOX
Ce code permet d'afficher un ToolTip lors du survol de la dropdwonlist d'un combobox. Cette fonctio...

{PHP} GÉNÉRATEUR DE LISTE DÉROULANTE
Voici une petite fonction sans prétention mais qui peut rendre des services :) Elle permet, à parti...

{PHP} LISTE DÉROULANTE AVEC TOUS LES PAYS
J'ai fait ce script parce que ben à priori je ne l'ai pas trouvé ici. C'est pas grand chose mais ca ...

{Javascript / DHTML} ENFIN UNE LISTE DEROULANTE (COMBOBOX) MODIFIABLE
J'ai longtemps cherché, et finalement j'ai fait mon propre truc, en reprenant qq sources de ce site....

{ColdFusion} RAFFRAICHIR UNE LISTE EN FONCTION D'UNE AUTRE
Ceci illustre la façon de raffraichir un champ de type liste déroulante en fonction d'une autre. ...

{Javascript / DHTML} BOITE DE DIALOGUE MODALE DE SÉLECTION D'UNE LISTE D'ICONES
La sélection d'une images dans une fenêtre est souvent effectuée via une fenêtre popup. L'inconvéni...

{C / C++ / C++.NET} UNE LISTE HÉTÉROGÈNE DOUBLEMENT CHAINÉE
Voici en C++ la gestion d'une liste doublement chainée où le type de chacun des maillons peut être d...

{C# / C#.NET} LISTER LES FICHIERS ET DOSSIER D'UN DOSSIER D'UN CLIC DROIT
Voici un programme qui va vous permettre de générer des arborescences textes. Elle permet notamment ...

{Visual Basic, VB6, VB.NET, VB 2005} ROUTINE DIR RÉCURSIVE POUR OBTENIR LA LISTE DE TOUS LES FICHIERS DANS UN RÉPERTOIRE ET SES SOUS-DOSSIERS AVEC LA TAILLE EN OCTETS
Utilise la structure TypeFichier (nom, repertoire, taille) Stockage de cette liste dans un tableau ...