我想使用ajax post从视图中传递codeigniter中的数据,在控制器中处理该数据并将结果数组发送回来查看

时间:2018-04-11 06:46:41

标签: javascript php ajax codeigniter

我搜索了整个WWW但没有得到任何帮助。任何解决方案将不胜感激。提前谢谢

我的HTML

 <div class="row">

我的案例中是否需要表格标签

        <form>
        <div class="form-group">
        <label for="date" class="col-12 col-md-2 control-label"><?php echo 'Date';?> 
        </label>
        <div class="col-12 col-md-3">
        <input type="text" class="datepicker form-control" name="date" id="date">
        </div>
        </div>

这里我从数据库中获取数据

<div class="form-group">
        <label for="class_id" class="col-12 col-md-2 control-label"><?php echo 
         get_phrase('Select Class');?></label>
         <div class="col-12 col-md-3">
         <select name="class_id" class="form-control" id="class_id" onchange="return get_attendance()" >
         <option value=""><?php echo get_phrase('select class');?></option>
         <?php 
         $classes = $this->db->get('class')->result_array();
            foreach($classes as $row):?>
               <option value="<?php echo $row['class_id'];?>"><?php echo $row['name'];?></option>
          <?php endforeach; ?>
          </select>
          </div>
         </div>
        </form>
    </div>

我正在使用datatable类进行导出和打印

    <table class="table table-bordered datatable" id="table_export">
        <thead>
             <tr>
                 <th>#</th>
                 <th><div><?php echo get_phrase('Status');?></div></th>
                 <th><div><?php echo get_phrase('Student Name');?></div></th>
                 <th><div><?php echo get_phrase('Class Name');?></div></th>
                 <th><div><?php echo get_phrase('Date');?></div></th>
                 <th><div><?php echo get_phrase('options');?></div></th>
             </tr>
         </thead>
         <tbody>
              <?php
                  $count = 1;
                  foreach($attendance as $row):?>
来自$ this-&gt; load-&gt;视图(&#39; backend / index&#39;,$ page_data)的控制器收到$出席次数;

<tr>
                          <td><?php echo $count++;?></td>
                          <td><?php echo $row['status'];?></td>
                          <td><?php echo $row['student_id'];?></td>
                          <td><?php echo $row['class_id'];?></td>
                          <td><?php echo $row['date'];?></td>
                       </tr>
                  <?php endforeach; ?>
           </tbody>
     </table>

我的JavaScript

以下脚本alert()class_id和date但不传递任何内容到控制器函数

<script type="text/javascript">
    function get_attendance() {

            var class_id = $('#class_id').val();
            var date = $('#date').val();

            $.ajax({
                type: "post",
                dataType:"json",
                data: {"class_id": class_id, "date": date},
                url: '<?php echo base_url();?>index.php?admin/manage_attendance',
                success: function (data) {

                }
            });
}


</script>

我的控制器

如果我将$date$class_id的静态值设为$date = '04-04-2018'; $class_id = 2; manage_attendance(),则可以使用$this->input->post()无效:

function manage_attendance()
    {
        if($this->session->userdata('admin_login')!=1)
            redirect(base_url() , 'refresh');


            $date = $this->input->post('date');
            $class_id = $this->input->post('class_id');

        $page_data['attendance']= $this->db->get_where('attendance', array('class_id' => $class_id , 'date' => $date))->result_array();

        $page_data['page_name']  =  'manage_attendance';
        $page_data['page_title'] =  get_phrase('manage_daily_attendance');
        $this->load->view('backend/index', $page_data);
    }

3 个答案:

答案 0 :(得分:0)

尝试更改

url: '<?php echo base_url();?>index.php?admin/manage_attendance' 

url: '<?php echo base_url();?>index.php/admin/manage_attendance',

答案 1 :(得分:0)

我可以通过以下编码得到相同的结果,但我不喜欢我想要的东西,因为我已经在上面发布了

<强> HTML

<div class="form-group">
        <label for="class_id" class="col-12 col-md-2 control-label"><?php echo 
         get_phrase('Select Class');?></label>
         <div class="col-12 col-md-3">
         <select name="class_id" class="form-control" id="class_id" onchange="return get_attendance()" >
         <option value=""><?php echo get_phrase('select class');?></option>
         <?php 
         $classes = $this->db->get('class')->result_array();
            foreach($classes as $row):?>
               <option value="<?php echo $row['class_id'];?>"><?php echo $row['name'];?></option>
          <?php endforeach; ?>
          </select>
          </div>
         </div>
        </form>
    </div>

<table class="table table-bordered datatable" id="table_export">
        <thead>
             <tr>
                 <th>#</th>
                 <th><div><?php echo get_phrase('Status');?></div></th>
                 <th><div><?php echo get_phrase('Student Name');?></div></th>
                 <th><div><?php echo get_phrase('Class Name');?></div></th>
                 <th><div><?php echo get_phrase('Date');?></div></th>
             </tr>
         </thead>
         <tbody id="putdatahere">
         </tbody>
</table>

的JavaScript

<script type="text/javascript">

    function get_attendance() {
        var e = document.getElementById("class_id");
        var class_id = e[e.selectedIndex].value;
        var date = $('#date').val();

        $.ajax({
            url: '<?php echo base_url();?>index.php?admin/manage_attendance/'+ date +'/' + class_id,
            success: function(response)
            {
                jQuery('#putdatahere').html(response);
               }
        });

    }
</script>

CodeIgniter控制器

function manage_attendance($date, $class_id){
      $query = $this->db->get_where('attendance', array('class_id' => $class_id , 'date' => $date))->result_array();

       if($query){
                        $html_return = "";
                        foreach ($query as $attendance) {
                            $html_return .="<tr><td>"; 
                            $html_return .= $attendance['status']; //further Proccessing here e.g if $attendance['status'] == 0 echo absent otherwise present
                            $html_return .="</td><td>";
                            $html_return .= $attendance['student_id']); //further Proccessing here e.g get student name from its student_id
                            $html_return .="</td><td>";
                            $html_return .= $attendance['class_id']; //further Proccessing here e.g get class name from its class_id
                            $html_return .= "</td><td>";
                            $html_return .= $attendance['date']; 
                            $html_return .="</td></tr>";
                        }

                        echo $html_return;
                    } else {
                        echo 'No record';
                    }
    }

答案 2 :(得分:0)

You have to append data like this using jquery:

function get_attendance() {
            var class_id = $('#class_id').val();
            var date = $('#date').val();
            $.ajax({
                type: "post",
                dataType:"json",
                data: {class_id: class_id, date: date},
            url: '<?php echo base_url();?>index.php?admin/manage_attendance',
                success: function (data) {

                var resultData = JSON.parse(response);

            if(resultData!=null){
                attendance= resultData.length;

            }
            if(attendance>0){

                for(i=0;i<attendance;i++){
                    $(".attendance_listing").append("<tr><td>"+resultData[i].status+"</td><td>"+resultData[i].student_id+"</td><td>"+resultData[i].class_id+"</td><td>"+resultData[i].date+"</td></tr>");
                }
            }


        }
    });
   }
   //controler
   function manage_attendance()
    {
        if($this->session->userdata('admin_login')!=1){
                redirect(base_url() , 'refresh');


                $date = $this->input->post('date');
                $class_id = $this->input->post('class_id');

                $page_data['attendance']= $this->db->get_where('attendance', array('class_id' => $class_id , 'date' => $date))->result_array();

                $page_data['page_name']  =  'manage_attendance';
                $page_data['page_title'] =  get_phrase('manage_daily_attendance');
                echo json_encode($page_data);
            }
    }