I have a select option that load categories from MySQL using php, i need when the user choose a value from the category Select, another select option appears and populates the products of this category. The two select should be in the same form because i need to pass the selected category and the selected product when submitting this form.
0
votes
2 Answers
1
votes
<select name='test1' id='test1'>
<option value='1'>1</option>
<option value='2'>3</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
Html
<select name='tes2' id='test2'>
</select>
Ajax
$("#test1").on('change',function(){
$.ajax({
url : 'someurl',
data: { data },
dataType: 'json',
async: false,
success: function(res){
//Populate second select
}
});
});
0
votes
I think the two commenters are referring to using Javascript to set the value of the element once the page has loaded.
The other option is to set the selected element on the server side e.g. html select option SELECTED