Q:

select option depends other option in wordpress admin page

I have two selections category and type, the type should come depending on the category selection, from the database, here is my code.
HTML
    $ps_type_table_name = $wpdb->prefix . 'ps_type'; ?>

    <form method="post" action="">
     <label>Category :</label><br>
         <select id="category" name="category" 
         class="form-control">
               <?php
         $categories = $wpdb->get_col('SELECT DISTINCT category FROM '.$ps_type_table_name);
                 foreach ($categories as $category){ ?>

               <option value="<?php echo $category; ?>"><?php echo $category; ?></option>
                   <?php
                      }
                      ?>
             </select><br><br>

             <label>Types :</label><br>
            <select id="type" name="type" class="form-control">

           </select><br><br>
   </form>

Ajax
$(document).ready(function(){
            $('#category').on('change',function(){
                var category = $(this).val();
                if(category){
                    $.ajax({
                        type:'POST',
                        url:'category-type-select.php',
                        data:'category_name='+category,
                            success:function (html) {
                        $('#type').html(html);
                    }
                    });
                }
            });
        });

PHP
 class Get_category_type{
 function __construct()
 {
if (isset($_POST['category_name']) && !empty($_POST['category_name'])) {
    global $wpdb;
    $category = $_POST['category_name'];
    $ps_type_table_name = $wpdb->prefix . 'ps_type';
    $types = $wpdb->get_col($wpdb->prepare('SELECT DISTINCT type FROM ' . $ps_type_table_name . ' WHERE category=%s', $category));

    foreach ($types as $type) {
        echo '<option value="' . $type . '">' . $type . '</option>';
    }
}
}

}

actually, this is a WordPress plugin using oops, please help me to solve this problem, I think the problem is getting PHP file in ajax code, please tell me how to write path in ajax URL to get that class file in WordPress plugin 

A:

I got solution for my problem so i posting answer to help some who needed, in WordPress, there is separate hook is there to use ajax function, and this procedure how to use it.
HTML
 $ps_type_table_name = $wpdb->prefix . 'ps_type'; ?>

<form method="post" action="">
 <label>Category :</label><br>
     <select id="category" name="category" 
     class="form-control">

           <?php
           $categories = $wpdb->get_col('SELECT DISTINCT category FROM '.$ps_type_table_name);
             foreach ($categories as $category){ ?>

       <option value="<?php echo $category; ?>"><?php echo $category; ?></option>

               <?php
                  }
                  ?>

         </select><br><br>

         <label>Types :</label><br>
        <select id="type" name="type" class="form-control">

       </select><br><br>

PHP code to get second option value by changing the first option
 public function get_type_by_category(){
    //echo file_get_contents('php://input'); for testing
    if (isset($_POST['ctgry']) && !empty($_POST['ctgry'])) {
        global $wpdb;
        $result = "";
        $category = $_POST['ctgry'];
        $ps_type_table_name = $wpdb->prefix . 'ps_type';
        $types = $wpdb->get_col($wpdb->prepare('SELECT DISTINCT type FROM ' . $ps_type_table_name . ' WHERE category=%s', $category));

        foreach ($types as $type) {
            $result .= '<option value="' . $type . '">' . $type . '</option>';
        }
        echo $result;
    }
}

Hook this function to wordpress
    add_action('wp_ajax_get_type_by_category','get_type_by_category');
    add_action('wp_ajax_nopriv_get_type_by_category','get_type_by_category');

Ajax.js
   $(document).ready(function () {
   $('#category').change(function(){
    var ctgry=$(this).val();
    $.ajax({
        type:"post",
        url:ajaxurl,
        data:{'action':'get_type_by_category','ctgry':ctgry},
        cache:false,
        success:function (html) {
            $("#type").html(html);
            //alert(html);
        },
        error:function(request,string_error,error){
            alert(string_error);
        }

    });
});
$('#category').change();
});

Hook js to WordPress
    public function enqueue_scripts(){
 wp_enqueue_script('ps-admin-page-js', plugins_url('js/admin-page.js', dirname(__FILE__)),array('jquery'),'1.1.1',true);
 }
 add_action('wp_footer', 'enqueue_scripts');

I found this way to use ajax in WordPress plugin development, and it works for me.

