I needed a dependent drop-down list, I found a similar solution with cities and regions.
I do not know how to make, so that from the selected variety , not only the quantity, but also the length values change?

Currently the amount of the selected grade is changing.

enter image description here

Here is the data file:

<?php $city = array ( 'ΠœΠ΅ΡΡ‚Π½Ρ‹Π΅ Ρ€ΠΎΠ·Ρ‹' => array ( 11 => '11 ΡˆΡ‚. (ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌ)', 25 => '25 ΡˆΡ‚.', 51 => '51 ΡˆΡ‚.', 101 => '101 ΡˆΡ‚.', ), 'ЭквадорскиС Ρ€ΠΎΠ·Ρ‹' => array ( 25 => '25 ΡˆΡ‚. (ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌ)', 51 => '51 ΡˆΡ‚.', 76 => '76 ΡˆΡ‚.', 101 => '101 ΡˆΡ‚.', ), ); ?> 

At the very beginning of the file:

 <?php require_once('city.php'); // ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ список с Π³ΠΎΡ€ΠΎΠ΄Π°ΠΌΠΈ $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ список Π³ΠΎΡ€ΠΎΠ΄ΠΎΠ² if ($action == 'getCity') { if (isset($city[$_GET['region']])) { echo json_encode($city[$_GET['region']]); // Π²ΠΎΠ·Π²Ρ€Π°Π°Ρ‰Π΅ΠΌ Π΄Π°Π½Π½Ρ‹Π΅ Π² JSON Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅; } else { echo json_encode(array('Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ количСство')); } exit; } // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ ΠΏΡ€ΠΈΡˆΠ΅Π΄ΡˆΠΈΠ΅ Π΄Π°Π½Π½Ρ‹Π΅ if ($action == 'postResult') { echo '<pre>' . htmlspecialchars(print_r($_POST, true)) . '</pre>'; exit; } ?> 

javascript:

  <script type="text/javascript"> // <![CDATA[ function loadCity(select) { var citySelect = $('select[name="city"]'); citySelect.attr('disabled', 'disabled'); // Π΄Π΅Π»Π°Π΅ΠΌ список Π³ΠΎΡ€ΠΎΠ΄ΠΎΠ² Π½Π΅ Π°ΠΊΡ‚ΠΈΠ²Π½Ρ‹ΠΌ // послыаСм AJAX запрос, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Π΅Ρ€Π½Ρ‘Ρ‚ список Π³ΠΎΡ€ΠΎΠ΄ΠΎΠ² для Π²Ρ‹Π±Ρ€Π°Π½Π½ΠΎΠΉ области $.getJSON('index.php', {action:'getCity', region:select.value}, function(cityList){ citySelect.html(''); // ΠΎΡ‡ΠΈΡ‰Π°Π΅ΠΌ список Π³ΠΎΡ€ΠΎΠ΄ΠΎΠ² // заполняСм список Π³ΠΎΡ€ΠΎΠ΄ΠΎΠ² Π½ΠΎΠ²Ρ‹ΠΌΠΈ ΠΏΡ€ΠΈΡˆΠ΅Π΄ΡˆΠΈΠΌΠΈ Π΄Π°Π½Π½Ρ‹ΠΌΠΈ $.each(cityList, function(i){ citySelect.append('<option value="' + i + '">' + this + '</option>'); }); citySelect.removeAttr('disabled'); // Π΄Π΅Π»Π°Π΅ΠΌ список Π³ΠΎΡ€ΠΎΠ΄ΠΎΠ² Π°ΠΊΡ‚ΠΈΠ²Π½Ρ‹ΠΌ }); } // ]]> </script> 

    1 answer 1

    Make one more level of nesting in the array, the rest of the server part will not change.

     $city = array ( 'ΠœΠ΅ΡΡ‚Π½Ρ‹Π΅ Ρ€ΠΎΠ·Ρ‹' => array ( 'quantity' => array ( 11 => '11 ΡˆΡ‚. (ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌ)', 25 => '25 ΡˆΡ‚.', 51 => '51 ΡˆΡ‚.', 101 => '101 ΡˆΡ‚.', ), 'lengths' => array ( 70 => '70 см', 90 => '90 см', ), ), 'ЭквадорскиС Ρ€ΠΎΠ·Ρ‹' => array ( 'quantity' => array ( 25 => '25 ΡˆΡ‚. (ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌ)', 51 => '51 ΡˆΡ‚.', 76 => '76 ΡˆΡ‚.', 101 => '101 ΡˆΡ‚.', ), 'lengths' => array ( 50 => '50 см', 80 => '80 см', ), ), ); 

    On the js side, fill the resulting two arrays cityList.quantity and cityList.lengths into the appropriate lists (just as you fill in the citySelect )

    I would do this: inside a getJSON(...) handler, I would write the following

     // ΠŸΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Π΅ΠΌ массив с ΠΊΠ»ΡŽΡ‡Π°ΠΌΠΈ Π² Π²ΠΈΠ΄Π΅ ΠΈΠΌΠ΅Π½ Π²Ρ‹ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΡ… списков $.each(cityList, function(field) { // ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Π½ΡƒΠΆΠ½Ρ‹ΠΉ список, ΠΎΡ‡ΠΈΡ‰Π°Π΅ΠΌ Π΅Π³ΠΎ ΠΈ Π²ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ var fieldElement = $('select[name="' + field + '"]') .empty() .removeAttr('disabled'); // ΠŸΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Π΅ΠΌ массив со значСниями ΠΏΡƒΠ½ΠΊΡ‚ΠΎΠ² списка $.each(this, function(i) { // БобствСнно наполняСм список зачСниями fieldElement.append('<option value="' + i + '">' + this + '</option>'); }); }); 

    In this case, selects corresponding to the number and size should be named according to the keys of the arrays that the server gives (that is, quantity and lengths ).

    • You can more, if not difficult. I can't figure it out ... - bakusite
    • @bakusite C what exactly? C JS? - tutankhamun
    • Yes, with filling out the lists, is it in JS? - bakusite
    • @bakusite completed the answer - tutankhamun