使用ajax搜索无法按日期工作

时间:2018-08-20 07:04:15

标签: php jquery ajax

我的问题是我正在使用ajax抛出ajax我想过滤数据但使用日期搜索无法正常工作

我的ajax代码插入了我的日期

$('.date').on('click', function (e) {
    e.preventDefault();

    var url = $(this).data("url");
    var form = $(this).data("form");
    var div = $(this).data("div");
    var table_url = $(this).data("table");
    console.log(form);
    var validationResp = $('.' + form).validation();
    console.log(validationResp);
    var form_data = new FormData($('.' + form)[0]);
    console.log(form_data);

    if (validationResp === '') {
        $.ajax({
            type: 'POST',
            url: answers,
            data: form_data,
            contentType: false,
            processData: false,
           // dataType: "text",
            cache: false,
            success: function (response) {
                response = JSON.parse(response);
                if (response.success) {
                    window.location.href = response.success;
                } else {
                    new PNotify({
                        title: 'Error!',
                        text: response.error,
                        type: 'error',
                        styling: 'bootstrap3'
                    });
                }
            },
            error: function (response) {
                console.log(response);
                new PNotify({
                    title: 'Error!',
                    text: "Something went wrong, Please try again.",
                    type: 'error',
                    styling: 'bootstrap3'
                });
            }
        })
    } else {
        new PNotify({
            title: 'Error!',
            text: validationResp,
            type: 'error',
            styling: 'bootstrap3'
        });

}

});

现在可以通过任何方式使用日期搜索数据

这是我的html部分的日期,我在其中输入日期,然后调用jquery或ajax

<form id="demo-form2"  method="post" Class="form-horizontal form-label-left sport_form">
                    <input type="date" name="date" class="date" id="date">
                   </form>

这是我的模型部分:-

    public function answersData($date) {
  $this->db->select('questions,options_names,user.first_name,user.last_name,athlete_name,date,user.user_id,user_answers.*');
                $this->db->from('user_answers');
                $this->db->join('question', 'question.id = user_answers.q_id');
                $this->db->join('options', 'options.op_id = user_answers.ans_respons', 'left');
                $this->db->join('user', 'user.user_id = user_answers.user_id','left');
                //$this->db->join('user_answers as us', 'us.date = '.$date, 'left');
               if($date != '1'){
                $this->db->where('user_answers.date',$date);
                 $query = $this->db->get();
                  return $result = $query->result_array();
               }else{

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

这是控制器部分

public function answers() {
        //$_POST['date']='2018-07-28';
         if(isset($_POST['date'])){
             $startDate =  $_POST['date'];
             $date1=date_create($startDate);
             $date = date_format($date1,"Y-m-d");
              $response = $this->Godspeed->answersData($date);
              $data['data']['question'] = $response;
       $this->load->template(array("Portal/user_answer" => ""), $data);
         }else{
            // $date = $_POST['date'];
        $response = $this->Godspeed->answersData($date='1');
        $data['data']['question'] = $response;
       $this->load->template(array("Portal/user_answer" => ""), $data);
       }

   }

0 个答案:

没有答案