Problèmes de 3 listes déroulantes liées AJAX

Résolu
NicoWebMaster -  
sipatsymasaka Messages postés 107 Statut Membre -
Bonjour les gens,

J'ai besoin d'aide, je veux créer 3 listes déroulantes liées en AJAX mais là je plante sur quelques petits soucis :

- La première liste déroulante s'affiche correctement, seulement, quand je fais une selection, la deuxième n'affiche pas ce qu'elle devrait afficher.

- Même chose pour la deuxième et la troisième.

Est-ce que quelqu'un pourrait m'aider ?

2 réponses

  1. NicoWebMaster
     
    J'ai oublié de vous mettre mes scripts!

    index.php

    <?php
    require './config.inc.php';

    $mysqli = new mysqli($db['server'], $db['user'], $db['passwd'], $db['name']);

    if (0 !== mysqli_connect_errno()) {
    exit('Could not connect to database: ' . mysqli_connect_error());
    }

    $sql = 'SELECT id_manufacturer, name FROM ps_manufacturer order by name';
    $sql_fam = 'SELECT cl.id_category, cl.name FROM ps_category_lang cl LEFT JOIN ps_category c ON cl.id_category = c.id_category WHERE c.level_depth = 3 order by name';
    $sql_sfam = 'SELECT cl.id_category, cl.name FROM ps_category_lang cl LEFT JOIN ps_category c ON cl.id_category = c.id_category WHERE c.level_depth > 3 order by name';
    ?>

    <script type="text/javascript" src="fabricants.js"></script>
    <script type="text/javascript" src="sousfamille.js"></script>

    <form action="submit.php" method="post" id="form">

    <p>Sélectionnez un fabricant:
    <select name="fabricants" id="fabricants" onchange="updateFamilles(this.value)">
    <option selected value="">Choisir un fabricant...</option>
    <?php
    if (FALSE !== ($res = $mysqli->query($sql))) {

    while ($row = $res->fetch_assoc()) {
    printf('<option value="%s">%s</option>', $row['id_manufacturer'], $row['name']);
    }
    $res->close();
    }
    ?>
    </select>
    </p>

    <p>Sélectionnez une famille:
    <select name="familles" id="familles" onchange="updateSousFamille(this.value)">
    <option selected value="">Choisir une famille...</option>
    <?php
    if (FALSE !== ($res = $mysqli->query($sql_fam))) {

    while ($row = $res->fetch_assoc()) {
    printf('<option value="%s">%s</option>', $row['id_category'], $row['name']);
    }
    $res->close();
    }
    ?>
    </select>
    </p>

    <p>Sélectionnez une sous-famille:
    <select name="sousfamille" id="sousfamille">
    <option selected value="">Choisir une sous-famille...</option>
    <?php
    if (FALSE !== ($res = $mysqli->query($sql_sfam))) {

    while ($row = $res->fetch_assoc()) {
    printf('<option value="%s">%s</option>', $row['id_category'], $row['name']);
    }
    $res->close();
    }
    ?>
    </select>
    </p>

    <p><input type="submit" value="Soumettre" />

    </form>

    rpc.php

    <?php

    if (TRUE === isset($_GET['fabricants'])) {
    $fabricants = intval($_GET['fabricants']);
    } else {
    exit;
    }
    if (($fabricants == "") or ($fabricants == " ")){
    exit;
    }
    else{
    require './config.inc.php';

    $mysqli = new mysqli($db['server'], $db['user'], $db['passwd'], $db['name']);

    if (0 !== mysqli_connect_errno()) {
    exit('Could not connect to database: ' . mysqli_connect_error());
    }

    header('Content-Type: text/xml; charset=UTF-8');

    $dom = new DOMDocument('1.0', 'utf-8');
    $message = $dom->createElement('message');
    $message = $dom->appendChild($message);

    $sql = 'SELECT id_category, name From ps_category_lang Where id_category IN (SELECT DISTINCT ps_category.id_parent FROM ps_category INNER JOIN ps_manufacturer_category_lien ON ps_category.id_category = ps_manufacturer_category_lien.id_category WHERE (((ps_manufacturer_category_lien.id_manufacturer_category_lien) Like '.$region.') AND ((ps_category.level_depth) Like 4)))';

    if (FALSE !== ($res = $mysqli->query($sql))) {

    while ($row = $res->fetch_assoc()) {
    $famille = $dom->createElement('famille', utf8_encode($row['name']));
    $famille = $message->appendChild($famille);
    $famille->setAttribute('id', $row['id_category']);
    }

    $res->close();
    }

    echo $dom->saveXML();
    }
    ?>

    sfam.php

    <?php
    if (TRUE === isset($_GET['departements'])){
    $departements = intval($_GET['departements']);
    } else {
    exit;
    }
    require './config.inc.php';

    $mysqli = new mysqli($db['server'], $db['user'], $db['passwd'], $db['name']);

    if (0 !== mysqli_connect_errno()) {
    exit('Could not connect to database: ' . mysqli_connect_error());
    }

    header('Content-Type: text/xml; charset=UTF-8');

    $dom = new DOMDocument('1.0', 'utf-8');
    $message = $dom->createElement('message');
    $message = $dom->appendChild($message);

    $sql = 'SELECT cl.id_category, cl.name FROM ps_category_lang cl LEFT JOIN ps_category c ON cl.id_category = c.id_category WHERE c.level_depth > 3 and c.id_parent = '.$departements.'';

    if (FALSE !== ($res = $mysqli->query($sql))) {

    while ($row = $res->fetch_assoc()) {
    $sousfamille = $dom->createElement('sousfamille', utf8_encode($row['name']));
    $sousfamille = $message->appendChild($sousfamille);
    $sousfamille->setAttribute('id', $row['id_category']);
    }

    $res->close();
    }

    echo $dom->saveXML();
    ?>

    sousfamille.js

    var ajax;

    try {
    ajax = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    ajax = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    if (typeof XMLHttpRequest!='undefined') {
    ajax = new XMLHttpRequest();
    }
    }
    }

    function updateSousFamille(/*region, */departements) {
    // ajax.open('get', 'rpc.php?region=' + region + '&departements=' + departements);
    ajax.open('get', 'rpc.php?departements=' + departements );
    ajax.onreadystatechange = handleResponse;
    ajax.send(null);
    }

    function handleResponse() {

    if(ajax.readyState == 4) {

    var data = ajax.responseXML.getElementsByTagName('sousfamilles');
    document.getElementById('sousfamille').innerHTML = '';
    for(var i=0;i<data.length;i++) {
    var option = document.createElement('option');
    option.setAttribute('value',data[i].getAttribute("id"));
    option.appendChild(document.createTextNode(data[i].firstChild.nodeValue));
    document.getElementById('sousfamille').appendChild(option);
    }
    }
    }

    function initForm() {
    // document.getElementById('regions').selectedIndex = 0;
    document.getElementById('departements').selectedIndex = 0;
    // updateSousFamille(document.getElementById('regions').value);
    updateSousFamille(document.getElementById('departements').value);
    }

    if (window.addEventListener) {
    window.addEventListener("load", initForm, false);
    } else if (window.attachEvent){
    window.attachEvent("onload", initForm);
    }

    fabricants.js

    var ajax;

    try {
    ajax = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    ajax = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    if (typeof XMLHttpRequest!='undefined') {
    ajax = new XMLHttpRequest();
    }
    }
    }

    function UpdateFamilles(fabricants) {
    ajax.open('get', 'rpc.php?fabricants=' + fabricants);
    ajax.onreadystatechange = handleResponse;
    ajax.send(null);
    }

    function handleResponse() {

    if(ajax.readyState == 4) {

    var data = ajax.responseXML.getElementsByTagName('familles');

    document.getElementById('famille').innerHTML = '';
    for(var i=0;i<data.length;i++) {

    var option = document.createElement('option');
    option.setAttribute('value',data[i].getAttribute("id"));
    option.appendChild(document.createTextNode(data[i].firstChild.nodeValue));
    document.getElementById('famille').appendChild(option);
    }
    }
    }

    function initForm() {
    document.getElementById('fabricants').selectedIndex = 0;
    updateFamilles(document.getElementById('fabricants').value);
    }

    if (window.addEventListener) {
    window.addEventListener("load", initForm, false);
    } else if (window.attachEvent){
    window.attachEvent("onload", initForm);
    }
    3
  2. sipatsymasaka Messages postés 107 Statut Membre 6
     
    on peut savoir comment t'as résolu?
    1