Kushal Srivastava Kushal Srivastava - 1 month ago 13
PHP Question

How to pass value from two select to one onchange function?

Hi i have multiple dynamic dropdowns. The values of the ahead dropdown is dependent on the previous dropdown value. I have a dropdown in which the value is dependent on the values from two dropdowns. The code for dropdowns.

<select name="batch" id="batch-list" onChange="getSubject(this.value);" class="form-control" required>
<option value="">Select Batch</option>
</select>

<select name="semester" id="semester-list" class="form-control" required>
<option value="">Select Semester</option>
</select>

//The value of the below select are dependent on the values from batch and semester
<select name="subject" id="subject-list" class="form-control" required>
<option value="">Select Semester</option>
</select>


The onchange function for single value:-

function getSubject(val) {
$.ajax({
type: "POST",
url: "dropdown.php",
data:'batch_id='+val,
success: function(data){
$("#subject-list").html(data);
}
});


}

The above onchange function only takes value from batch how do i pass the value for semester in the fuction via ajax.

Answer

Try this:

function getSubject(param1, param2) {
       $.ajax({
            type: "POST",
            url: "dropdown.php",
            data: {
                val1 : param1,
                val2 : param2
            },
            success: function(data){
            $("#subject-list").html(data);
        }
}

and call it like:

$(document).on('change', '#dropdown1_id', function(){
    var dd1 = $(this).val();
    var dd2 = $('#dropdown2').val();

    getSubject(dd1, dd2);
});