无法将数据从数据库读入下拉列表

时间:2017-06-13 08:05:45

标签: php jquery mysql sql

我正在尝试从数据库中读取数据。我无法从数据库加载部分并在依赖的多选下拉列表中插入部分。

我的观点(drop_view):

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" />
  <script src="jquery-ui-multiselect-widget-master/src/jquery.multiselect.js"></script>
  <link rel="stylesheet" href="jquery-ui-multiselect-widget-master/jquery.multiselect.css" />
</head>
<script>
  $(document).ready(function() {
    $("#student").change(function() {
      //alert($(this).val()) 
      $.post("http://localhost/Dropdown/index.php/Drop_control/class", {
        student: $(this).val()
      }, function(data) {
        $("#class").html(data);

      });
    });
  });
</script>
<script>
  $(document).ready(function() {
    $("#class").change(function() {
      //alert($(this).val()) 
      $.post("http://localhost/Dropdown/index.php/Drop_control/section", {
        class: $(this).val()
      }, function(data) {
        $("#section").html(data);

      });
    });
  });
</script>
<body>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
  <link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />
  <script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" type="text/javascript"></script>

  <div id="mine">
  </div>

  <script type="text/javascript">
    $(function() {
      $('#section').multiselect({
        includeSelectAllOption: false;
      });
      $('#btnSelected').click(function() {
        var selected = $("#section option:selected");
        var message = "";
        selected.each(function() {
          message += $(this).text() + " " + $(this).val() + "\n";
        });
        alert(message);
      });
    });
  </script>
  <script>
    /*$(document).ready(function(){
         $('#section').multiselect({
          nonSelectedText: 'Select Section',
          enableFiltering: true,
          enableCaseInsensitiveFiltering: true,
          //buttonWidth:'400px'
         });

         $('#table_frame').on('submit', function(event){
          event.preventDefault();
          var form_data = $(this).serialize();
          $.ajax({
          // url:"insert.php",
           method:"POST",
           data:form_data,
           success:function(data)
           {
            $('#section option:selected').each(function(){
             $(this).prop('selected', false);
            });
            $('#section').multiselect('refresh');
            alert(data);
           }
          });
         });


        });*/
  </script>


  <table class="table table-bordered">
    <tr>
      <td>
        <div align="center">Select CSV</div>
      </td>
      <td>
        <div align="center">:</div>
      </td>
      <td>

        <select name="selectcsv" id="student">
           <option value="" selected="selected" >Select option</option>
          <option value="AllStudents" >All Students</option>
          <option value="Select_Specfic_class">Select_Specfic_class</option>
          </select>
      </td>
    </tr>

    <tr>
      <td>
        <div align="center">Class</div>
      </td>
      <td>
        <div align="center">:</div>
      </td>
      <td>
        <select name="class" id="class">
           <option selected="selected">Select Class</option>
           </select>
      </td>
    </tr>

    <tr>
      <td>
        <div align="center">Section</div>
      </td>
      <td>
        <div align="center">:</div>
      </td>
      <td id="table_frame">
        <select id="section" multiple="multiple">
            <option selected="selected">Select Section</option>
          </select>//here i am getting problem
      </td>
    </tr>

  </table>

</body>

</html>

我的模型(drop_model):

<?php 

class Drop_model extends CI_Model
{
    function __constuct()
    {
        parent::__constuct();
    }
    public function class1()
    {
        $this->db->select('class');
        $this->db->distinct();
        $res=$this->db->get('student_details');
        return $res->result();
    }

    public function section1($class)
    {
        $this->db->select('section');

        $this->db->where('class',$class);
                 $this->db->distinct();
        $res=$this->db->get('student_details');
        return $res->result();
    }
}

我的控制器(Drop_control):

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Drop_control extends CI_Controller
{

    public function index()
    {
        $this->load->view('drop_view');
    }
    public function class()
    {
         $student=$this->input->post('student');

         $this->load->model('drop_model');
         $user=$this->drop_model->class1();
         print_r($user);

         echo '<option value="" selected="selected">Select class</option>';
         foreach($user as $c){

        echo '<option value="'.$c->class.'">'.$c->class.'</option>';
         }



    }
    public function section()
    {
         $class=$this->input->post('class');

         $this->load->model('drop_model');
         $usr=$this->drop_model->section1($class);


         foreach($usr as $s){

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

         }



    }
}
?>

1 个答案:

答案 0 :(得分:0)

您无法在以下版本(&lt; -5.6.x)中使用class名称作为您的功能名称。见this。另外,从控制器的类中删除print_r($user);