doufang7385 2013-10-25 15:15
浏览 52
已采纳

无法在控制器或模型中获取ajax调用提交表单的输入

I have an ajax call for a form submit; it works fine if I pass my sql arguments when I hard code them, however if I want to pass my sql query arguments with inputs (from View) in my model it says: Message: Undefined index: startDate and endDate.

Here is my View:

            <?PHP
                 $formData2 = array(
                    'class' => 'form-horizontal',
                     'id' => 'frm2',
                   );
                  echo form_open('gallery/fetchAssociates', $formData2);
            ?>
            <input id="startDate" class="span2" size="16" type="text"  />
            <input id="endDate" class="span2" size="16" type="text"  />                                 
            <input type="submit" class="btn btn-primary" 
             value="Submit" id="querystartEnd" name="querystartEnd" />                 
            <?PHP
            echo form_close();
            ?>

and my javascript for AJAX call is as following:

       $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
?>",
            url: "<?php echo base_url('gallery/fetchAssociates') ?>",

            success: function(data) {

                html = "<table id='myTable'><thead><tr id='test'><th>ID</th><th>Start Date</th><th> LName</th></tr></thead><tbody id='contentTable'>";

                for (var i = 0; i < data.length; i++)
                {
                    html = html + "<tr  id='trResponses' ><td><div >" + data[i]['id']
                                + " </div></td><td><div >" + data[i]['start'] + 
                                   "</div> </td><td><div >" + data[i]['username'] +
                                    "</div></td></tr>";
                  }
                  html = html + "</tbody></table>";
                  $("#resultFrm2").html(html);
               },
              error: function() 
               {
                   alert('error');
               }

             });

and here is my controller:

    public function fetchAssociates() {

        //echo($_POST["startDate"]);
        //echo($_POST["endDate"]);
        //die();                       
        $this->load->model('user_model');
        echo json_encode($this->user_model->getAll());

    }

and my Model method is as following:

public function getAll() 
{
    $wtc = $this->load->database('wtc', TRUE);
    $sql = "SELECT username, MIN(timeIn) as start
             FROM tc_timecard
             GROUP BY userid  having  MIN(timeIn) > ? and MIN(timeIN) < ? 
             order by MiN(timeIN);";

     //$q = $wtc->query($sql, array('2013-01-08', '2013-01-23'));
     $q = $wtc->query($sql, array($this->input->post('startDate'),
      $this->input->post('endDate')));

     if ($q->num_rows() > 0) 
        {
            foreach ($q->result() as $row) 
            {
                $data[] = $row;
             }

             return $data;
         }
 }

As you see my comments in my code: if I have

 //echo($_POST["startDate"]);
//echo($_POST["endDate"]);

uncommented in firebug in response it says "Message: Undefined index: startDate and endDate." Also in my controller if I have

//        $q = $wtc->query($sql, array('2013-01-08', '2013-01-23'));

un-commented it works but once I want to pass the inputs by the following line of code it does not work :

$q = $wtc->query($sql, array($this->input->post('startDate'), $this->input->post('endDate')));

What can be the reason that I cannot access my inputs in my controller or Model?

If it is not clear for you, please let me know which part you need more clarification.

EDITED: It is worth mentioning that my ajax call is inside the following block:

  $('#frm2').submit(function(e) 
     {
            e.preventDefault();
            //My AJAX call here
     });

Many thanks in advance,

  • 写回答

1条回答 默认 最新

  • doushi3715 2013-10-25 15:31
    关注

    You are not passing any data through ajax

    // Collect data from form which will be passed to controller
    var data = {
     start_data : $('#startDate').val(),
     start_data : $('#endDate').val(),
    }
    
    $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
            data : data // data here
            url: "<?php echo base_url('gallery/fetchAssociates') ?>",
            success : function(data){
             // Success code here
            },
            error: function(data){
             // error code here
            }
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包