Hi,

I want to run php function using jquery ajax,
Task is
i have two dropdown(html select)

Filter_by_class.php

  <form action="filter.php" method="POST" id="filter_students_form"> <div class="field"> <label for="stud_class_filter" class="cbx-lbl lbl-width">Select Class</label> <span class="dbl-dote">:</span> <select name="studclass_filter" id="stud_class_filter"> <option value="">-- SELECT STUDENT CLASS --</option> <?php
                        if ($getStudentClass = $db->query("SELECT * FROM classes")) {

                            if ($getStudentClass->num_rows) {
                                while ($row = $getStudentClass->fetch_object()) {
                                    ?> <option value="<?php echo $row->classkey; ?>"><?php echo $row->standard; ?></option> <?php
                                }
                            }
                        }
                        ?> </select> </div> <div class="field"> <label for="stud_section_filter" class="cbx-lbl lbl-width">Select Section</label> <span class="dbl-dote">:</span> <select name="studsection_filter" id="stud_section_filter"></select> </div> <div class="textalignCenter"> <input type="submit" value="Submit" name="class_select" id="class_select" class="btn"> </div> </form>   

When i choose the SELECT STUDENT CLASS relevant sections of class loaded automatically in section dropdown
<select name="studsection_filter" id="stud_section_filter"></select>

For example 1st Standard to 6 th Standard to has A,B,C,D Sections

And 7th Standard to 12 th Standard has only A,B Sections

main.js File

$(function () {
    var filter_list_select_id = 'stud_class_filter'; 
    var filter_list_target_id = 'stud_section_filter'; 

    var filter_initial_target_html = '<option value="">Please Select Section</option>'; 

    $('#'+filter_list_target_id).html(filter_initial_target_html);


     $('#'+filter_list_select_id).change(function() {
        var selectvalue = $(this).val();

        if (selectvalue == "") {
            $('#'+filter_list_target_id).html(filter_initial_target_html);
        } else {
            $.ajax({
                type: "POST",
                url: "filter_load_sections.php",
                data: { studclass_filter : selectvalue },
                success: function(output) {
                    $('#'+filter_list_target_id).html(output);
                }

            });

        }
    });
});

filter_load_sections.php

<?php 

require_once 'db/db_connect.php';
require_once 'functions/functions.php';

// Loading Relavent Class Sections For Filter_by_class.php 

$section_record = array();

if (isset($_POST['studclass_filter'])) {

    $class_selected_filter = $_POST['studclass_filter'];

    $slct_sections = $db->prepare("SELECT section_groups.section, sections.section_key FROM classes, section_groups, sections WHERE classes.section_group = section_groups.section_group AND sections.section = section_groups.section AND classes.classkey = ?") or die($db->error);

    $slct_sections->bind_param('i', $class_selected_filter);

    $slct_sections->execute();

    $slct_section_result = $slct_sections->get_result();

    if ($slct_section_result->num_rows) {

        echo '<option value="">Please select...</option>';

        while($row = $slct_section_result->fetch_object()) {

        echo '<option value="'.$row->section_key.'">'.$row->section."</option>";

        }
        $slct_section_result->free();

    } 
} else {
    header('Location: filter_by_class.php');
}

?>

the above codes for filter_by_class.php

the above codes are working fine but i have 2 files called filter_by_class.php and add_student.php

i need to load the relevant sections in two files so i am using these codes two times,

but i want to merge filter_load_sections.php, load_sections_action.php and run from my functions.php how to do this ????

Note mysqli query is same so that am asking...

add_student.php

 <div class="field"> <label for="stud_class" class="cbx-lbl lbl-width">Select Student Class</label> <span class="dbl-dote">:</span> <select name="studclass" id="stud_class"> <option value="">-- SELECT STUDENT CLASS --</option> <?php
            if ($getStudentClass = $db->query("SELECT * FROM classes")) {

                if ($getStudentClass->num_rows) {
                    while ($row = $getStudentClass->fetch_object()) {
                        ?> <option value="<?php echo $row->classkey; ?>"><?php echo $row->standard; ?></option> <?php
                    }
                }
            }
            ?> </select> </div> <div class="field"> <label for="stud_section" class="cbx-lbl lbl-width">Select Student Section</label> <span class="dbl-dote">:</span> <select name="studsection" id="stud_section"></select> </div>

Inside main.js

$(function() {
    var list_select_id = 'stud_class'; 
    var list_target_id = 'stud_section'; 

    var initial_target_html = '<option value="">Please select Section</option>'; 

    $('#'+list_target_id).html(initial_target_html);


     $('#'+list_select_id).change(function() {
        var selectvalue = $(this).val();

        if (selectvalue == "") {
            $('#'+list_target_id).html(initial_target_html);
        } else {
            $.ajax({
                type: "POST",
                url: "load_sections_action.php",
                data: { studclass : selectvalue },
                success: function(output) {
                    $('#'+list_target_id).html(output);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status + " "+ thrownError);
                }
            });

        }
    });
});

load_sections_action.php

<?php 

require_once 'db/db_connect.php';
require_once 'functions/functions.php';

// Loading Relavent Sections in add_students.php

$section_record = array();

if (isset($_POST['studclass'])) {

    $class_selected = $_POST['studclass'];

    $slct_sections = $db->prepare("SELECT section_groups.section, sections.section_key FROM classes, section_groups, sections WHERE classes.section_group = section_groups.section_group AND sections.section = section_groups.section AND classes.classkey = ?") or die($db->error);

    $slct_sections->bind_param('i', $class_selected);

    $slct_sections->execute();

    $slct_section_result = $slct_sections->get_result();

    if ($slct_section_result->num_rows) {

        echo '<option value="">Please select...</option>';

        while($row = $slct_section_result->fetch_object()) {

            echo '<option value="'.$row->section_key.'">'.$row->section."</option>";

        }
        $slct_section_result->free();   
    } 
} else {
    header('Location: add_students.php');
}

?>