Mostafa Mohsen
Mostafa Mohsen

Reputation: 786

How to populate a selectbox depending on another selectbox using PHP JSON ARRAY

I'm trying to poplulate a select box that is dependent on another select box

<select>
    <option value="a">a</option>
    <option value="b">b</option>
</select>



<select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

I want for example when a user selects the select1 (a), Select2 shows 1, 2 and 3 and when he chooses B, It shows 4,5,6 and so on.

The selectbox values is a PHP ARRAY object, I've tried jQuery to populate it using $.each but that didn't work out as the array is something like:

{ "Option 1":[ {"name" : "test", "age" : "1"}, .... ] }

Thanks in advance.

Upvotes: 0

Views: 69

Answers (4)

Mostafa Mohsen
Mostafa Mohsen

Reputation: 786

Thanks guys for all the help however, I followed another approach. which is to output the whole PHP array in the select as options, and hide them all, and when an option in select1 is chosen, I use jQuery to only show the items that has a specific attribute:

<script>
$(".select2").prop("disabled", true);
$(".select1").change(function() {
$('.select2').prop('disabled', false);
$(".select2 option").hide();
var currentVal = $('.select1 :selected').attr('optionis'),
ValueSelected = $(".select2 option[optionvalue*='" + currentVal + "']");
ValueSelected.show();
$(ValueSelected).first().prop("selected", true); 
});
</script>
<select class="select1">
  <option value="1" optionis="1">Option 1</option>
  <option value="2" optionis="2">Option 2</option>
</select>
<select class="select2">
  <option value="1" optionvalue="1">Value 1</option>
  <option value="2" optionvalue="1">Value 2</option>
  <option value="1" optionvalue="2">Value 3</option>
  <option value="1" optionvalue="2">Value 4</option>
  <option value="1" optionvalue="2">Value 5</option>
</select>

CODEPEN

Upvotes: 0

mshomali
mshomali

Reputation: 654

For example you have below json:

{
    "Apple": {
        "iphone SE": "https://www.apple.com/iphone-se/",
        "iphone 7": "https://www.apple.com/iphone-7/"
    },
    "samsung": {
        "galaxy s8": "http://www.samsung.com/us/explore/galaxy-s8/",
        "galaxy s7": "http://www.samsung.com/us/explore/galaxy-s7/"
    }
}

For this we must to use javascript and It's good to use jQuery(a library for javascript).Take a look at the example below:

$(function() {
    var selectValues = {
        "apple": {
            "iphone SE": "https://www.apple.com/iphone-se/",
            "iphone 7": "https://www.apple.com/iphone-7/"
        },
        "samsung": {
            "galaxy s8": "http://www.samsung.com/us/explore/galaxy-s8/",
            "galaxy s7": "http://www.samsung.com/us/explore/galaxy-s7/"
        }
    };

    var $vendor = $('select.mobile-vendor');
    var $model = $('select.model');
    $vendor.change(function() {
        $model.empty().append(function() {
            var output = '';
            $.each(selectValues[$vendor.val()], function(key, value) {
                output += '<option>' + key + '</option>';
            });
            return output;
        });
    }).change();

    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>  
    <select class="mobile-vendor">
    <option value="apple">Apple</option>
    <option value="samsung">Samsung</option>
  </select>

<select class="model">
    <option></option>
</select>
</p>

Upvotes: 1

Ronn Wilder
Ronn Wilder

Reputation: 1368

// you can try something like this.

var s1 = document.getElementById("s1");
var s2 = document.getElementById("s2");

var s1Value = s1.value;

var i;
var frag = document.createDocumentFragment();
var ele;
if(s1Value == 'a'){
    for(i=0;i<3;i++){
        ele = document.createElement('option');
        ele.value = i + 1;
        ele.innerHTML = i + 1;
        frag.appendChild(ele);
    }
}
s2.innerHTML = ''
s2.appendChild(frag);

Upvotes: 0

lit
lit

Reputation: 460

Here is an example of how to implement the change with jquery.

Please post/explain your objects format a little more and I will create a loop to parse it correctly.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<select id="letter">
    <option value="a">a</option>
    <option value="b">b</option>
</select>

<select id="age">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>
  <script>
  $("#letter").change(function(){
    if($("#letter").val() === "a"){
      $("#age")
        .empty()
        .append('<option selected="selected" value="1">1</option>')
      ;
    }else{
      $("#age")
        .empty()
        .append('<option selected="selected" value="5">5</option>')
      ;
    }
  });
  </script>
</body>
</html>

Upvotes: 0

Related Questions