使用ajax和codeigniter通过控制器将数据发送到模型

时间:2016-07-12 10:52:52

标签: javascript php ajax codeigniter

我可以知道为什么我在控制台上收到此错误消息(500(内部服务器错误))。桌上没有任何东西出现。我想根据将使用JavaScript发送的信息在表格上显示数据

/ * MODEL * /

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


    class User extends CI_Model
    {

        function __construct() {
            parent::__construct();
            $this->load->database();       
        }

        public function validate_user($email, $password) {
            $this->db->distinct();
            $this->db->select('c.contact_id, name, email, password, level, c.statusid');
            $this->db->from('contact c');
            $this->db->join('deliveryreg d', 'c.contact_id = d.idcontact');
            $this->db->join('ClientLogin cl', 'cl.idcontact = c.contact_id');
            $this->db->where('c.statusid = 1');
            $this->db->where('email', $email);
            $this->db->where('password', $password); 
            $this->db->limit(1); 

            $query = $this->db->get();

           if($query->num_rows() == 1)
           {
             return $query->result();
           }
           else
           {
             return false;
           }
            }



        public function getSearch($data){


            $this->db->distinct();
            $this->db->select(" s.id AS 'searchid',s.description as 'searchname',so.id AS 'sourceid',so.description as 'service'");
            $this->db->from('contact c');
            $this->db->join('deliveryreg d', 'c.contact_id = d.idcontact');
            $this->db->join('searchsource ss', 'ss.id = d.idsearchsource');
            $this->db->join('search s', 's.id = ss.idsearch');
            $this->db->join('source so', 'so.id = ss.idsource');
            $this->db->where('c.statusid = 1'); 
            $this->db->where('email', $data['email']);
            $ids = array('1','5','8');
            $this->db->where_in('so.id', $ids);
            $this->db->order_by('s.description', 'so.description');

            $query = $this->db->get(); 
            return $query->result();
        }

        public function printData(){

            $db2 = $this->load->database('otherdb', TRUE);
            $db3 = $this->load->database('pan', TRUE);

            $sql = "SELECT c.id,p.name AS 'Pubname',section,pubdate,byline,headline,subheadline,TEXT,AREA,ave,linkedto,thumimage,pagenum,clipuploadpath, c.articleref,pdf,docpath,articlepartnumber,articlepartscount,keyword,logo,p.description AS 'PubInfo',circulation,m.description AS 'MediaType', r.description FROM pearhpmszm_db5.Allocation a JOIN pearhpmszm_db5.clip c ON c.id = a.idclip LEFT JOIN pearhpmszm_db3.Publication p ON c.pubid = p.id LEFT JOIN pearhpmszm_db3.ABC ab ON ab.idpub = p.id
                LEFT JOIN pearhpmszm_db3.mediatype m ON m.id = p.idmediatype LEFT JOIN pearhpmszm_db3.Region r ON r.id = p.idregion WHERE a.idsearch IN ('".'$_POST["idsearch"]'."') AND c.pubdate BETWEEN '".'$_POST["dateFrom"]'."' AND '".'$_POST["dateTo"]'."'";

            $query = $this->db->query($sql);
            return $query->result();

        }

        public function onlineData(){

            $db2 = $this->load->database('otherdb', TRUE);
            $db4 = $this->load->database('pan2', TRUE);

            $sql = "SELECT a.idsearch AS 'Searchid',c.id AS 'clipid',c.url,headline,c.description,pubdate,author,TEXT,ave,p.name AS 'sitename', st.description AS 'sitetype',r.description AS 'region' FROM pearhpmszm_db4.allocation a JOIN pearhpmszm_db4.clip c ON c.id = a.idclip LEFT JOIN pearhpmszm_db3.site p ON c.siteid = p.id LEFT JOIN pearhpmszm_db3.sitetype st ON st.id = p.sitetypeid LEFT JOIN pearhpmszm_db3.Region r ON r.id = p.regionid WHERE a.idsearch IN ('".'$Searchid'."') AND c.pubdate BETWEEN '".'$dateFrom'."' AND '".'$dateTo'."' ";

            $query = $this->db->query($sql);
            return $query->result();

        }

        public function broadcastData(){

            $db2 = $this->load->database('otherdb', TRUE);
            $db5 = $this->load->database('pan3', TRUE);

            $sql = "SELECT mentions.id,`pearhpmszm_db2`.`search`.Description,pearhpmszm_db6.Allocation.idsearch,Mention,station,MentionDT,duration,AVE,clipPath,  au.Audience,pearhpmszm_db3.Region.Description AS 'Region',pearhpmszm_db3.language.language,pearhpmszm_db6.station.idlanguage,pearhpmszm_db6.licenceType.Licence,  (SELECT programme FROM  pearhpmszm_db6.schedule WHERE  idstation = pearhpmszm_db6.station.id AND CASE DAYOFWEEK(mentiondt) WHEN 1 THEN  sun = 1  WHEN 2 THEN  mon = 1  WHEN 3 THEN  tue = 1  WHEN 4 THEN  wed = 1  WHEN 5 THEN  thu = 1 WHEN 6 THEN  fri = 1   WHEN 7 THEN  sat = 1 END AND starttime < TIME(mentiondt) AND endtime > TIME(mentiondt) LIMIT 1 ) AS Programme, pearhpmszm_db6.Allocation.keyword FROM pearhpmszm_db6.Allocation  JOIN pearhpmszm_db6.mentions ON pearhpmszm_db6.Allocation.idmention = pearhpmszm_db6.mentions.ID   JOIN `pearhpmszm_db2`.`search` ON `pearhpmszm_db6`.`Allocation`.idsearch = `pearhpmszm_db2`.`search`.id  LEFT JOIN pearhpmszm_db6.station ON pearhpmszm_db6.station.name = pearhpmszm_db6.mentions.Station  LEFT JOIN pearhpmszm_db3.Audience au  ON au.idstation = `pearhpmszm_db6`.station.id AND au.active = 1  LEFT JOIN pearhpmszm_db3.Region ON pearhpmszm_db6.station.IDRegion = pearhpmszm_db3.Region.ID   LEFT JOIN pearhpmszm_db3.language ON pearhpmszm_db6.station.idlanguage = pearhpmszm_db3.language.id   LEFT JOIN pearhpmszm_db6.licenceType ON pearhpmszm_db6.licenceType.id = pearhpmszm_db6.station.idlicencetype   LEFT JOIN pearhpmszm_db6.programname  ON au.idstation = `pearhpmszm_db6`.station.id   WHERE pearhpmszm_db6.Allocation.idsearch IN ('".'$idsearch'."') AND pearhpmszm_db6.mentions.MentionDT BETWEEN '".'$dateFrom'."' AND '".'$dateTo'."'";

            $query = $this->db->query($sql);
            return $query->result();

        }

        function __destruct() {
                $this->db->close();
            }
    }

/ * CONTROLLER * /

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class PrintData extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('user');

 }

 function index()
 {
   if($this->session->userdata('logged_in'))
   {
     $session_data = $this->session->userdata('logged_in');
     $data['email'] = $session_data['email'];
     $data['query'] = $this->user->getSearch($data);
     $data['print'] = $this->user->printData($data);
     $this->load->view('header', $data);
     $this->load->view('sidebar', $data);
     $this->load->view('print_content', $data);
     $this->load->view('footer', $data);
   }
   else
   {
     //If no session, redirect to login page
     redirect('login', 'refresh');
   }
 }

 function getPrintData($data){
  if($this->session->userdata('logged_in'))
   {
       $session_data = $this->session->userdata('logged_in');
       $data['email'] = $session_data['email'];
       $data['idsearch'] = $this->user->printData($data);
       $data['dateFrom'] = $this->user->printData($data);
       $data['dateTo'] = $this->user->printData($data);
       $data['query'] = $this->user->getSearch($data);
       $data['print'] = $this->user->printData($data);

   }
   else
   {
     //If no session, redirect to login page
     redirect('login', 'refresh');
   }
 }

 function logout()
 {
   $this->session->sess_destroy('logged_in');
   session_destroy();
   redirect('home', 'refresh');
 }

}

?>

/ *查看* /     

      <!-- Add the sidebar's background. This div must be placed
           immediately after the control sidebar -->
      <!-- <div class="control-sidebar-bg"></div> -->
    </div><!-- ./wrapper -->

    <!-- REQUIRED JS SCRIPTS -->

    <!-- jQuery 2.1.4 -->
    <script src="<?php echo base_url();?>assets/js/jQuery-2.1.4.min.js"></script>
    <!--Jquery-->
    <script src="<?php echo base_url();?>assets/js/jquery.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script>
    <!-- AdminLTE App -->
    <script src="<?php echo base_url();?>assets/js/app.min.js"></script>
    <!-- Scroll effect -->
    <script src="<?php echo base_url();?>assets/js/scroll.js"></script>
    <!-- Slide effect -->
    <script src="<?php echo base_url();?>assets/js/slideEffect.js"></script>

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>

    <script>


      /*******************************************
          GET THE LAST 24HRS DATA PER SEARCHID
      ********************************************/

      function getPrintData(){
        alert("In getPrintData");

          var dateFrom = moment().add(-1,'days').format("DD/MM/YYYY");
          var dateTo = moment().format("DD/MM/YYYY");
          var searchid = "";
          var count = 0;
            searchid = $('.searchid:checked').map(function () {
                count = count + 1;
                return this.value;
            }).get().join(",");


          $.ajax({
            url:"<?php echo site_url('printdata/getPrintData');?>",
            data:{dateFrom:dateFrom, dateTo:dateTo, idsearch:searchid},
            type:"POST",
            cache: false,
            success: function (response, status, xhr) {
                        alert(response);
                        $("#myContentPrint").html(response);

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(errorThrown);
                    }
          });
        }





    </script>



    <!-- Optionally, you can add Slimscroll and FastClick plugins.
         Both of these plugins are recommended to enhance the
         user experience. Slimscroll is required when using the
         fixed layout. -->
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

首先捕获ajax在控制器中发送的数据,如

  function getPrintData(){
    if($this->session->userdata('logged_in'))
      {
           $data = array('dateFrom' => $this->input->post('dateFrom'),
                 'dateTo' => $this->input->post('dateTo'),
                 'idsearch' => $this->input->post('idsearch'),
           );

       $session_data = $this->session->userdata('logged_in');
       $data['email'] = $session_data['email'];
       $data['idsearch'] = $this->user->printData($data);
       $data['dateFrom'] = $this->user->printData($data);
       $data['dateTo'] = $this->user->printData($data);
       $data['query'] = $this->user->getSearch($data);
       $data['print'] = $this->user->printData($data);

     }
     else
    {
        //If no session, redirect to login page
        redirect('login', 'refresh');
    }
  }

秒您的模型应使用传递给模型方法的参数

来接收数据

但您没有通过任何论据

它应该是这样的:

  function printData($data = array()){

        $db2 = $this->load->database('otherdb', TRUE);
        $db3 = $this->load->database('pan', TRUE);

        $sql = "SELECT c.id,p.name AS 'Pubname',section,pubdate,byline,headline,subheadline,TEXT,AREA,ave,linkedto,thumimage,pagenum,clipuploadpath, c.articleref,pdf,docpath,articlepartnumber,articlepartscount,keyword,logo,p.description AS 'PubInfo',circulation,m.description AS 'MediaType', r.description FROM pearhpmszm_db5.Allocation a JOIN pearhpmszm_db5.clip c ON c.id = a.idclip LEFT JOIN pearhpmszm_db3.Publication p ON c.pubid = p.id LEFT JOIN pearhpmszm_db3.ABC ab ON ab.idpub = p.id  LEFT JOIN pearhpmszm_db3.mediatype m ON m.id = p.idmediatype LEFT JOIN pearhpmszm_db3.Region r ON r.id = p.idregion WHERE a.idsearch IN ('".'$data["idsearch"]'."') AND c.pubdate BETWEEN '".'$data["dateFrom"]'."' AND '".'$data["dateTo"]'."'";

        $query = $this->db->query($sql);
        return $query->result();

       }

而不是使用$ _POST在printData中使用$ data($ data = array())

答案 1 :(得分:0)

首先在控制器中获取数据:

function getPrintData(){
    if($this->session->userdata('logged_in'))
      {
           $data = array('dateFrom' => $this->input-post('dateFrom'),
                 'dateTo' => $this->input-post('dateTo'),
                 'idsearch' => $this->input-post('idsearch'),
           );
        $this->user->printData($data);

       $session_data = $this->session->userdata('logged_in');
       $data['email'] = $session_data['email'];
       $data['idsearch'] = $this->user->printData($data);
       $data['dateFrom'] = $this->user->printData($data);
       $data['dateTo'] = $this->user->printData($data);
       $data['query'] = $this->user->getSearch($data);
       $data['print'] = $this->user->printData($data);

     }
     else
    {
        //If no session, redirect to login page
        redirect('login', 'refresh');
    }
  }

您将获得模型中的数据:

public function printData($data){
//your code here
}