使用Ajax从index.php页面将下拉选项选择的值发送到codeigniter中的search.php

时间:2019-03-16 11:42:36

标签: php mysql ajax codeigniter

                This is my controller 

                    function search($collg_id = '', $id = '') {
                      $courserecord = $this->front->get_data_wheree('tbl_course_offered');
                      $data['course_offrd_name'] = json_decode(json_encode($courserecord), True);
                      if (empty($data['course_offrd_name'])) {
                        $data['course_offrd_name'] = json_decode(json_encode($courserecord), True);
                      } else {
                        $courserecord = $this->front->get_data_wheree('tbl_course_offered');
                        $data['course_offrd_name'] = json_decode(json_encode($courserecord), True);
                      }
                    }


                This is my model.php

                    function get_data_wheree($table) {
                      $this->db->select('course_offrd_name, collg_id');
                      $this->db->group_by('course_offrd_name');
                      return $this->db->get('tbl_course_offered')->result();
                    }

                This is my source view page ie index.php dropdown page

                    <form class="form-horizontal well bar" action="<?php echo base_url();?>search" method="post" role="form">
                      <div class = "col-md-2">
                        <div class = "form-group">
                          <div class = "col-md-12">
                            <select class = "course" name = "course_offrd-name[]" id = "course_offrd_name" class = "form-control">
                              <option value = "">Select</option>
                              <?php
                                foreach ($coursedata as $val) {
                                ?>              
                                  <option value="<?php echo $val->course_offrd_name; ?>">
                                  <?php echo $val->course_offrd_name; ?>
                                  </option> 
                                <?php
                               }
                              ?>
                            </select>
                          </div>
                        </div>
                      </div> 
                    </form>
                    <script>
                      $(document).ready(function () {
                        $("select.course").change(function () {
                          var selectedCourse = $(this).children("option:selected").val();
                          alert("You have selected the course - " + selectedCourse);
                        });
                      });
                    </script>



                <--this is my  second view page ie search.php where myselected option should display--> 

                    <form class="form-horizontal well" action="<?php echo base_url(); ?>search" method="post" role="form">
                      <span>
                        <?php foreach ($coursedata as $val) {
                        ?>
                        <p>
                        <input class="with-gap filter_course CheckCourse" name="course_offrd_name[]" multiple type="checkbox" id="<?php echo $val->course_offrd_name; ?>"  value="<?php echo $val->course_offrd_name ?>" 
                        <?php
                          if (!empty($courserecord))
                          if (count($courserecord) == 1)
                          if ($courserecord[0]['course_offrd_name'] == $val->course_offrd_name) {
                            echo $courserecord[0]['course_offrd_name'];
                            echo "checked";
                          } else {
                        }
                        ?> />
                        <label for="<?php echo $val->course_offrd_name ?>"></label>
                        </p>
                        <?php } ?> 
                      </span>
                     </form>
                This is my ajax code 
                <script type="text/javascript">
                  jQuery(document).ready(function($) {
                        $(".course").on('change', function() 
                        {

                        var level = $(this).val();
                        if(level)
                        {


                            $.ajax ({
                                type: 'POST',
                                url: base_url+"home/search",
                                data: { course_name: 'course_name' },
                                success : function(htmlresponse) 
                                {

                                    $('#opt_lesson_list').html(htmlresponse);
                                    console.log(htmlresponse);
                                }
                            });
                        }
                    });
                });
                </script>

                This is my home controller index function  page
                public function index()
                    {
                        if($this->session->userdata('user_login'))
                        {
                                if($this->input->post())
                                {

                                $data   = array();
                                $data['state_name']=$this->input->post('state');
                                $data['country_name']=$this->input->post('country');
                                $data['course_name']=$this->input->post('course_name');
                                $data['sub_cat_id']=$this->input->post('subcategory_name');
                                $data['catgy_id']=$this->input->post('category_name');
                                $data['collg_name']=$this->input->post('collg_name');

                                }
                $coursedata= $this->front->get_data_wheree('tbl_course_offered');
                                    $data['coursedata']=$coursedata;


            This is search function in home controller 
            public function search($collg_id='',$id='')
                {
                    if($this->session->userdata('user_login'))
                    {

                                if($this->input->post())
                                {

                                $data = array();
                                $data['email']=$this->input->post('email');
                                $data['password']=$this->input->post('password');
                                $data['isactive']=1;
                                $data['collg_id']=$this->input->post('collg_id');
                                $where=array();


                                    if($this->input->post('course_name')!='')
                                    {
                                        $where['tbl_college.course_name']=$this->input->post('course_name');
                                        $data['course_name']=$this->input->post('course_name');

                                    }
                                    $where['isactive']=1;

                                    $select='`tbl_college`.`collg_id`,`collg_name`,`logo_img`,`header_img`,`phone_no`,`state`,`country`,`city`,`tbl_college`.`isactive`,`pdf_name`,`tbl_college.category_name`,`tbl_college.subcategory_name`,`tbl_college.course_name`';
                                        /
                                    $record=$this->get_uniquedata($this->front->selected_data($select,'tbl_college',$where),'collg_id');

                                    $data['record']= json_decode(json_encode($record), True);


                                   if(empty($data['record'])){

                                        $this->session->set_flashdata("success","No Records found");
                                         redirect(base_url());
                                    }
                                    else{
                                                     $data['subcategoryrecord']= json_decode(json_encode($subcategorydata), True);              }
                                                     $courserecord = $this->front->get_data_wheree('tbl_course_offered');


                                                      $data['course_offrd_name'] = json_decode(json_encode($courserecord), True);

                                                     if(empty($data['course_offrd_name']))
                                                      {

                                                     $data['course_offrd_name'] = json_decode(json_encode($courserecord), True);
                          }
                          else 
                           {
                $courserecord = $this->front->get_data_wheree('tbl_course_offered');
                                         $data['course_offrd_name'] = json_decode(json_encode($courserecord), True);


                           }
            }
            }
                                else{

                                    $record = $this->front->get_data_where('tbl_college',array('isactive'=>'1'));
                                        //$data['record']= json_decode(json_encode($record), True);
                                //  echo "<pre>";print_r($data['record']);die;
                                    $data['record']=$record;
                                    }

                        $data['record']=$record;
            $coursedata = $this->front->get_data_wheree('tbl_course_offered');
                        $data['coursedata']=$coursedata;
            $this->load->view('home/search',$data);
            }






                I want to send the dropdown option selected values from one view page to another view page in CodeIgniter using ajax or jQuery. 

                The option value which is selected should be sent from index.php to search.php. It should just display the value name in search.php 
                If required will provide more code. How can be it done?

                Now I have code in search() function. I have made changes in code. Please see this search function.
                how to do it using CodeIgniter and ajax, how we can send the dropdown option already selected and send from one view to another view page using CodeIgniter and Ajax

                if need more code will be provided. 
            I have added the ajax code and controller index function 
            check the above code ....if required more code will post 
            Thanks in advance
            I have added the ajax code and controller index function  code 
            I have added the ajax code and controller index function code 
            I have added the ajax code and controller index function code 
    I have added the search() of home controller .
    I have added the search() of home controller .
I have added the search() of home controller .
I have added the search() of home controller .
        I have added the search() of home controller .
        If required more code will provide
        I have added the search() of home controller .
        I have added the search() of home controller .
        I have added the search() of home controller .

我已经添加了home controller的search()。 我已经添加了home controller的search()。 如果需要,将提供更多代码 预先感谢M.hemant对我的代码感兴趣。 如果您需要更多代码或表结构,将提供 预先感谢M.hemant对我的代码感兴趣。 如果您需要更多代码或表结构将提供

1 个答案:

答案 0 :(得分:0)

我相信(*这里有很多代码,如果不是,请原谅我),但是我相信问题是因为您在做:

data: { course_name: 'course_name' },

何时应该做:

data: { course_name: selectedCourse },

您发送的是“ course_name”的文本/字符串,而不是您选择的下拉列表中的实际值。