php dynamic select 2foption from mysql table

Solutions on MaxInterview for php dynamic select 2foption from mysql table by the best coders in the world

showing results for - "php dynamic select 2foption from mysql table"
Marta
25 Nov 2016
1First, add an id to age group
2
3<select name="age_group" id="age_group">
4Then
5
6$( document ).ready(function() {
7    $('#age_group').change(function(){
8        $.ajax({
9            type: "POST",
10            url: "ajax.php",
11            data: { group_id: $(this).val() },
12            dataType: "html"
13        })
14        .done(function( msg ) {
15            $('#hobbies').html(msg);
16        });
17    });
18});
19In ajax.php you get all hobbies where group_id = $_POST['group_id']
20
21$sql = "SELECT name FROM tbl_hobbies WHERE group_id = ".$_POST['group_id'];
22// Execute your query and put the result in a variable (example $result_query)
23// Then loop it
24foreach($result_query as $row) {
25    echo '<option>'.$row['name'].'</option>';
26}
27NB : The loop may change according to your method to retrieve data