Site icon Hip-Hop Website Design and Development

use `foreach()` in ajax name

I’ve written a code for retrieving knowledge from the database utilizing the ajax for WordPress. The next code is the one written within the capabilities.php. The code is for getting the id and names of subcategories underneath a class id which is outlined as $cat . The consequence, i must show as a drop down record

perform fetchData(){
  world $wpdb;
    $catId = $_POST['key']; // worth from the ajax

        // Now we wish to JSON encode these values to ship them to $.ajax success.
      if($catId){
         $result_fromDB = $wpdb->get_results("SELECT * FROM  sub_category the place categor_id = '".$catId."'");
          echo "<choice worth='".$catId."' >".$catId."</choice>"; //This line is returned in ajax. this can be a take a look at code
         foreach ($result_fromDB as $subcat) {
                           echo "<choice worth='".$subcat->id."' >".$subcat->sub_category_name."</choice>"; // This line doesnt returned, getting solely **<choice></choice>** in console
          }
      }

        die();
    }

The code equivalent to the ajax is,

jQuery.ajax({
    kind: 'POST',
    url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
    knowledge: {
         'key' : catId,
        'motion': "fetch_data" // essential
    },
    success : perform (knowledge) {
             console.log(knowledge);
             jQuery('#sub_cat').html(knowledge);
            }
  });

Can anybody give the explanation and reply for not getting the info within the foreach()/loop