简体   繁体   English

通过在codeigniter php中使用ajax,根据第一个下拉结果填充第二个下拉列表

[英]populate 2nd drop down based on 1st drop down result by using ajax in codeigniter php

I'm trying to do the 2nd drop down population based on the 1st drop down by using ajax in codeigniter.我正在尝试通过在 codeigniter 中使用 ajax 来根据第一个下拉列表进行第二个下拉填充。 But my problem is after I choose for 1st drop down value, when I try to choose for 2nd drop down value, it shows empty only.但我的问题是在我选择第一个下拉值后,当我尝试选择第二个下拉值时,它只显示为空。

Image below shows that the result after I choose for 1st option.下图显示了我选择第一个选项后的结果。 When I try to select for 2nd option, it can't shows the value:当我尝试选择第二个选项时,它无法显示值: 这是我面临的问题

Here's is the code for view:这是查看代码:

    <div class="container">
<div class="container h-100">
    <div class="row h-100 justify-content-center align-items-center">
    <div class="card">
                <div class="card-body">
                    <h3 class="card-title text-center"><?php echo $content_heading;?></h3>
                    <form action="#" id="form" novalidate>
                        <div class="form-row">
                            <div class="col-sm-12">
                                <div class="form-group">
                                <label for="subjectname">Subject Name</label>
                                <select class="form-control" name="subjectname" id="subjectname">
                                   <option>-- Select Subject Name --</option>
                                   <?php foreach($attendance as $row){
                                     echo "<option value='".$row['subject_id']."'>".$row['subject_name']."</option>";
                                   }
                                   ?>
                                </select>
                                    <div class="invalid-feedback"></div>
                                </div>
                                <div class="form-group">
                                    <label for="lecturer_name">Lecturer Name</label>
                                    <input name="lecturer_name" placeholder="Lecturer Name" class="form-control" type="text" disabled="">
                                    <div class="invalid-feedback"></div>
                                </div>
                                <div class="form-group">
                                    <label for="subjectsection">Section</label>
                                     <select  class="form-control" name = "subjectsection" id='subjectsection'>
                                       <option>-- Select Subject Section --</option>
                                    </select>
                                    <div class="invalid-feedback"></div>
                                </div>
                                <div class="form-group">
                                    <label for="attendance_date">Date</label>
                                    <input name="attendance_date" placeholder="Date" class="form-control" type="date">
                                    <div class="invalid-feedback"></div>
                                </div>
                                <div class="form-group">
                                    <label for="attendance_start_time">From</label>
                                    <input name="attendance_start_time" placeholder="From" class="form-control" type="time">
                                    <div class="invalid-feedback"></div>
                                </div>
                                <div class="form-group">
                                    <label for="attendance_end_time">To</label>
                                    <input name="attendance_end_time" placeholder="To" class="form-control" type="time">
                                    <div class="invalid-feedback"></div>
                                </div>
                        <button type="button" id="btnSave" class="btn btn-primary btn-block" onclick="save()">Confirm</button>
                    </div>
                </div>
            </form>

        </div>
    </div>
</div>

Here is the javascript for the ajax function.这是 ajax 函数的 javascript。

 <script> $(document).ready(function(){ // Subject change $('#subjectname').change(function(){ var subject_name = $(this).val(); // AJAX request $.ajax({ url : '<?php echo site_url('attendance/get_subject_section')?>', method: 'post', data: {subject_name: subject_name}, dataType: 'json', success: function(response){ // Remove options $('#subjectsection').find('option').not(':first').remove(); // Add options $.each(response,function(index,data){ $('#subjectsection').append('<option value="'+data['subject_id']+'">'+data['subject_section']+'</option>'); }); } }); }); }); </script>

Here is the code for controller:这是控制器的代码:

    <?php class attendance extends CI_Controller{
function __construct(){
    parent::__construct();
    $this->load->model('Attendance Data/attendance_model','attendance');
}

function index(){
    //$this->load->view('Manage User Registration/user_login_view');
    $data['meta_title'] = 'Attendance';
    $data['content_heading'] = 'Attendance';
    $data['main_content'] = 'Manage Student Attendance/attendance';
    $data['user_id'] = $this->session->userdata('id');

    if($this->session->userdata('user_type')==='lecturer'){
        $data['user_type'] = 'lecturer';
        $data['meta_user_level_title'] = 'Lecturer';
        $data['id'] = $this->session->id;
        $data['main_content'] = 'Manage Student Attendance/lecturer_attendance_view';
        $this->load->view('template/template', $data);
    }else{
        echo "Access Denied";
    }
}

    public function create_attendance()
{
    $data['meta_title'] = 'Add Subject Attendance';
    $data['content_heading'] = 'Add Subject Attendance';
    $data['main_content'] = 'Manage Student Attendance/attendance';
    $data['user_id'] = $this->session->userdata('id');
    $data['user_type'] = 'lecturer';
    $data['heading'] = 'Lecturer';

    if($this->session->userdata('user_type')==='lecturer'){
        $data['user_type'] = 'lecturer';
        $data['attendance'] = $this->attendance->get_subject_name();
        $data['meta_user_level_title'] = 'Lecturer';
        $data['id'] = $this->session->id;
        $data['main_content'] = 'Manage Student Attendance/create_attendance';
        $this->load->view('template/template', $data);
    }else{
        echo "Access Denied";
    }
}

    public function get_subject_section()
{ 
    // POST data 
    $subject_name = $this->input->post('subject_name');
    
    // get data 
    $data = $this->attendance->get_subject_section($subject_name);
    echo json_encode($data); 
}}

Here is my code for model:这是我的模型代码:

<?php class attendance_model extends CI_Model{

    //Get subject name
    public function get_subject_name()
{
    $user_id = $this->session->userdata("id");
    $response = array();
 
    // Select record
    $this->db->select('*');
    $this->db->where('lecturer_id', $user_id);
    $this->db->group_by('subject_id');
    $query = $this->db->get('lecturer_timetable');
    $response = $query->result_array();

    return $response;
}

    //Get subject section
    public function get_subject_section($subject_name){
        $user_id = $this->session->userdata("id");
        $response = array();
     
        // Select record
        $this->db->select('subject_id, subject_section');
        $this->db->where('lecturer_id', $user_id);
        $this->db->where('subject_name', $subject_name);
        $query = $this->db->get('lecturer_timetable');
        $response = $query->result_array();

        return $response;
    }}

Can someone help me to solve this problem?有人可以帮我解决这个问题吗? Thanks.谢谢。

your url in ajax is unreadable because of quotation.由于引用,您在 ajax 中的 url 不可读。 Try to writer in proper quotation.尝试以适当的引语写作。 Use either single quotes inside double quotes or use double inside single.在双引号内使用单引号或在单引号内使用双引号。 Second is, you are using method as 'POST' but in lowercase.其次,您使用的方法是“POST”,但使用小写。 Write POST in uppercase.用大写写 POST。

$.ajax({    
    url : "<?php echo site_url('attendance/get_subject_section')?>",
    method: 'POST',
    data: {subject_name: subject_name},
    dataType: 'json',
    success: function(response){        
        console.log(response); // first check by console that you are getting records in response. once you get response then append it or do whatever you want
    
    }

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据第一个下拉值使第二个下拉列表自动选择值 - How to make 2nd drop down list auto select value based on 1st drop down value 与第二下拉选择相关的第二下拉选择 - 2nd Drop Down selection to be related to the selection on the 1st Drop Down Selection 在第一个下拉菜单被选中后显示第二个下拉菜单的内容。 - Show content of 2nd Drop down after 1st Drop down getting selected. 如何根据第一个下拉列表过滤第二个下拉列表 - how to filter 2nd drop-down list according to 1st drop-down list 如何在第一个选择上禁用(或更改颜色)第二个下拉选项? - How to disable (or change color) of 2nd drop down option depenging on 1st selection? 显示基于First Drop Down PHP JS的第二个下拉列表 - Show 2nd Dropdown based on First Drop Down PHP JS 根据PHP中选择的第一个下拉列表填充第二个下拉列表 - Populate 2nd dropdown based on 1st dropdown selected in PHP 我想做一个条件,如果我从第一个下拉列表中选择这个项目,它会只显示我在第二个下拉列表中选择的项目 - I want to make a condition in which if I select this item from 1st drop down show it shows me only selected items in 2nd drop down 使用ajax和PHP基于第一个选择框填充第二个选择框 - populating a 2nd select box based off the 1st one using ajax and PHP 显示基于第一个下拉列表的第二个下拉列表不起作用 - Showing 2nd Dropdown based on First drop down not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM