Question

I have a sql query to which I want to pass date using DatePicker. I did the parameter binding and it works if I give values directly to the binded variables. I don't know how to exactly pass the value after clicking submit button. In my view I use TbActiveForm and datepicker widgets. So my view file link.php looks like:

<?php
$form = $this->beginWidget(
    'bootstrap.widgets.TbActiveForm',array(
    'id' => 'inlineForm',
    'type' => 'inline',
    'htmlOptions' => array('class' => 'well'),
));
?>
Start Date
<?php $this->widget('bootstrap.widgets.TbDatePicker',array(
                 'name' => 'from_date',
                 'options' => array('format' => 'yyyy-mm-dd')
)); 
?>
End Date           
<?php $this->widget('bootstrap.widgets.TbDatePicker',array(
                'name' => 'to_date',
                'options' => array('format' => 'yyyy-mm-dd')
  ));

?>
&nbsp;
<!-- Submit Button -->   
<?php $this->widget('bootstrap.widgets.TbButton',array(
                'buttonType' => 'Submit',
                'label' => 'Submit',
                 'type' => 'primary',
                 'url' => 'Link'  
 )); 

  $this->endWidget();

And in the controller I have :from_date and :to_date binded to variables $from and $to respectively. If I provide values to the variables the query works fine so there is no problem in binding. The controller code looks like:

  public function actionLink()
      {

    // $from = $_GET['from_date'];
    // $to = $_POST['to_date'];
$from=!empty($_POST['from_date'])?$_POST['from_date']:'2014-01-01';
    $to=!empty($_POST['to_date'])?$_POST['to_date']:'2014-02-01';

 $sql = "SELECT [Ordernumber]
  ,[Order_Date]

  FROM [Orders]
  WHERE CAST(Order_Date As Date) BETWEEN :from_date AND :to_date
  ORDER BY Order_Date ";
 $sql2 = "SELECT COUNT(*)
        FROM [Orders]
        WHERE CAST(Order_Date As Date) BETWEEN :from_date AND :to_date";
         $connection=Yii::app()->db;
         $command=$connection->createCommand($sql2); 

       //   $command = $connection->createCommand('SELECT COUNT(*) FROM (' . $sql . ') as count_alias');
          $command->bindParam(':from_date',$from,PDO::PARAM_STR);
          $command->bindParam(':to_date',$to,PDO::PARAM_STR);
          $dataProvider = new CSqlDataProvider($sql, array('keyField' => 'Order_Date',

            'params' => array(
                ':from_date' => $from,
                 ':to_date' => $to),
            'totalItemCount' => $command->queryScalar(),

            'pagination' => array(
                'pageSize' => 20)));


    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'
      $this->render('link',  array('dataProvider' => $dataProvider));
}

So if I just use:

 $from = $_POST['from_date'];
 $to = $_POST['to_date'];

I get an error: undefined index from_date.

I want to know what code I have to write for Submit button so it passes the values from DatePicker back to the controller. And how the controller reads the value.

Right now if I click on Submit Button it reloads the page but by the else values of the

    $from=!empty($_POST['from_date'])?$_POST['from_date']:'2014-01-01';
    $to=!empty($_POST['to_date'])?$_POST['to_date']:'2014-02-01'; 
Was it helpful?

Solution 2

It was a syntax error. I wrote:

'buttonType' => 'Submit',

Instead of:

'buttonType' => 'submit',

Thanks for all the help guyz.

OTHER TIPS

undefined index from_date.

Because your trying to retrieve it from a get rather than a post

 $from = $_GET['from_date'];
 $to = $_POST['to_date'];

should be

 $from = $_POST['from_date'];
 $to = $_POST['to_date'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top