如何在Codeigniter中使用下拉菜单过滤数据

时间:2019-04-07 19:07:35

标签: php jquery json codeigniter

我现在正在开发管理员端,并使用模板过滤器创建表单以调用模板和模板中包含的呼叫页面

现在我被下拉过滤器卡住了,我的jQuery没有调用控制器函数

我不确定是因为模板,还是某些代码错误

Matchcon控制器

public function matchscrbatcon()
    {
        $data['player']=    $this->Matchmodel->getAllPlayer();
        $data['team']=  $this->Matchmodel->getAllTeam();
        $data['match']= $this->Matchmodel->getAllmatch();
        $data['page'] = 'match/matchscrbat';
        $this->load->view('templates/form_template', $data);
        if($this->input->post('save'))
        {
            $insmsbdata=array('msbat_id'=>'',
          'match_id'=>$this->input->post('minfmname'),
            'team_id'=>$this->input->post('mteamid'),
            'player_id'=>$this->input->post('msbplayerid'),
            'inning_1or2'=>$this->input->post('msbing'),
            'run'=>$this->input->post('msbrun'),
            'ball'=>$this->input->post('msbball'),
            'strike_rate'=>$this->input->post('msbsr'),
            'four'=>$this->input->post('msb4'),
            'six'=>$this->input->post('msb6'),
            'wicket_by'=>$this->input->post('msbwb'),
            'caught_by'=>$this->input->post('msbcb'),
            'stumped_by'=>$this->input->post('msbsb'),
            'runout_direct'=>$this->input->post('msbrodb'),
            'runout1'=>$this->input->post('msbro1'),
            'runout2'=>$this->input->post('msbro2'));
            $this->Matchmodel->savemsbrecords($insmsbdata);
            echo '<script>alert("You Have Successfully updated this Record!");</script>';
        }
    }
    function getteambymatch($matchid)
    {
        header('Content-Type: application/x-json; charset=utf-8');
                echo(json_encode($this->Matchmodel->getAllplayingteam($matchid)));
    }

匹配模型我的模型

 function savemsbrecords($insmsbdata)
      {
        $this->db->insert('matchscore_bat',$insmsbdata);
      }
    function getAllSeries()
      {
        $query=$this->db->query('SELECT series_id, series_name FROM series');
        return $query->result();
      }
      function getAllPlayer()
      {
        $query=$this->db->query('SELECT player_id, player_name FROM player');
        return $query->result();
      }
      function getAllTeam()
      {
        $query=$this->db->query('SELECT team_id, team_name FROM team');
        return $query->result();
      }
      function getAllVenue()
      {
        $query=$this->db->query('SELECT venue_id, venue_name FROM venue');
        return $query->result();
      }
      function getAllmatch()
      {
        $this->db->select("match_id, match_name, mdate");
        $this->db->from('match');
        $query=$this->db->get();
        return $query->result();
      }
     function getAllplayingteam($matchid)
      {
        $this->db->select("team.team_id, team.team_name,match.match_id");
        $this->db->from('team');
        $this->db->join('match','match.team1=team.team_id OR match.team2=team.team_id');
        $this->db->where('match.match_id',$matchid);
        $query=$this->db->get();
        return $query->result();
      }

matchscrbat视图文件

<section class="content">
      <div class="row">
          <!-- general form elements -->
          <div class="box box-warning">
            <div class="box-header with-border">
              <h3 class="box-title">Player Details</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <form method="post" role="form">
                <!-- text input -->
                <div class="form-group">
                  <label>Match</label>
                  <select class="form-control select2" name="minfmname" style="width: 100%;">
                    <?php
                    foreach ($match as $key =>$value) {
                      echo '<option value="'.$value->match_id.'">'.$value->match_name.' date:'.$row->mdate.'</option>';
                    }?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Team</label>
                  <select class="form-control select2" name="mteamid" style="width: 100%;">
                  </select>
                </div>
                <div class="form-group">
                  <label>Player</label>
                  <select class="form-control select2" name="msbplayerid" style="width: 100%;">
                    <option selected="selected"></option>
                    <?php
                    foreach ($player as $row) {
                      echo '<option value="'.$row->player_id.'">'.$row->player_name.'</option>';
                    }?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Inning 1 or 2</label>
                  <input type="text" name="msbing" class="form-control" placeholder="Enter...">
                </div>
                <div class="form-group">
                  <label>Run</label>
                  <input type="text" name="msbrun" class="form-control" placeholder="Enter ...">
                </div>
                <div class="form-group">
                  <label>Ball</label>
                  <input type="text" name="msbball" class="form-control" placeholder="Enter ...">
                </div>
                <div class="form-group">
                  <label>Strike Rate</label>
                  <input type="text" name="msbsr" class="form-control" placeholder="Enter...">
                </div>
                <div class="form-group">
                  <label>Four</label>
                  <input type="text" name="msb4" class="form-control" placeholder="Enter...">
                </div>
                <div class="form-group">
                  <label>Six</label>
                  <input type="text" name="msb6" class="form-control" placeholder="Enter...">
                </div>
                <div class="form-group">
                  <label>Wicket By</label>
                  <select class="form-control select2" name="msbwb" style="width: 100%;">
                    <option selected="selected"></option>
                    <?php
                    foreach ($player as $row) {
                      echo '<option value="'.$row->player_id.'">'.$row->player_name.'</option>';
                    }?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Caught By</label>
                  <select class="form-control select2" name="msbcb" style="width: 100%;">
                    <option selected="selected"></option>
                    <?php
                    foreach ($player as $row) {
                      echo '<option value="'.$row->player_id.'">'.$row->player_name.'</option>';
                    }?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Stumped by</label>
                  <select class="form-control select2" name="msbsb" style="width: 100%;">
                    <option selected="selected"></option>
                    <?php
                    foreach ($player as $row) {
                      echo '<option value="'.$row->player_id.'">'.$row->player_name.'</option>';
                    }?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Ranout Direct By</label>
                  <select class="form-control select2" name="msbrodb" style="width: 100%;">
                    <option selected="selected"></option>
                    <?php
                    foreach ($player as $row) {
                      echo '<option value="'.$row->player_id.'">'.$row->player_name.'</option>';
                    }?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Ranout Player 1</label>
                  <select class="form-control select2" name="msbro1" style="width: 100%;">
                    <option selected="selected"></option>
                    <?php
                    foreach ($player as $row) {
                      echo '<option value="'.$row->player_id.'">'.$row->player_name.'</option>';
                    }?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Ranout Player 2</label>
                  <select class="form-control select2" name="msbro2" style="width: 100%;">
                    <option selected="selected"></option>
                    <?php
                    foreach ($player as $row) {
                      echo '<option value="'.$row->player_id.'">'.$row->player_name.'</option>';
                    }?>
                  </select>
                </div>
                <td>
                  <input type="submit" name="save" value="Submit" class="btn btn-block btn-primary btn-lg">
                </td>
              </form>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
        <!--/.col (right) -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
  </div>
<script src="http://localhost/cd/assets/bower_components/jquery/dist/jquery.min.js"></script>
<!-- jQuery -->
<script src="http://localhost/cd/assets/bower_components/jquery/dist/jquery.js"></script>

我的jQuery

  <script type="text/javascript">

    $(document).ready(function() {
        $('select[name="minfmname"]').on('change', function() {
            var matchID = $(this).val();
            if(matchID) {
                $.ajax({
                    url: '<?php echo site_url('matchcon/getteambymatch');?>'+matchID,
                    type: "POST",
                    dataType: "json",
                    success:function(teams) {
                        $('select[name="mteamid"]').empty();
                        $.each(teams, function(key, value) {
                            $('select[name="mteamid"]').append('<option value="'+ value.team.team_id +'">'+ value.team.team_name +'</option>');
                        });
                    }
                });
            }else{
                $('select[name="mteamid"]').empty();
            }
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

jQuery

printf("%18s%s", hash[e_cols[i]], OFS)