Passing Variable Thru Buttons To Controller Not Working

Hi, banging my head on the wall on this one. I’m trying to update my gridviewer(for a message board) according to different dates on the click of a button.

for example in my view :

{input date} {Button} {ajax Button}

<div id="data">

{[Gridview according to dates]}

</div>

then everytime i click the {button} or {ajax Button} the gridviewer should filter only messages for the dates according to {input date}. My problem now is my action doesn’t see the variable ($data) passed by either CHtml:Button, or CHtml:AjaxButton.

my Button code in my view file:




<div class="form">

<?php 

$this->widget('zii.widgets.jui.CJuiDatePicker', array( // using datepicker to get input date

			'model' => $model,

			'attribute' => 'start_date',

.....


));


		 $data["myDate"] = $model->start_date; 

                 var_dump($data);

  echo CHtml::button('Click me', array('submit' => array('message/viewMessageBoardByDate', array('data'=>$data))));


  echo CHtml::ajaxButton(

						'Index submit button',

            array('message/viewMessageBoardByDate'), //controller 

            array('update'=>'#data')	 

        );



the var_dump($data) gives "array(1) { ["myDate"]=> NULL }".

if i change my button to :




		echo CHtml::button('Click me', array('submit' => array('message/viewMessageBoardByDate','data'=>$data)));

Also the same.

now in my Message controller :




    public function actionViewMessageBoardByDate()

    {  

                var_dump($data);

		$this->renderPartial('/message/viewMessageBoardOnly',array('data'=>$data));

	}	



Right now clicking the {button} shows Undefined variable: data . clicking the {ajax Button} gives no response at all, not even error…

In my view file i have a CGridView widget to show the messages according to the input date.(should be no problem with that. Ok when i tested it separately)

Am i passing the variables wrong? is there a special way to do it using buttons? Both buttons and datepicker are inside a form. I do have a <div> with id ‘data’ for the ajaxbutton to update the content. This post seems to do it ok.

Any idea on how to troubleshoot ? or is there a better way to do the function?

Thanks in advance.

[EDIT]

Just realized that variables passed this way are accessed using the $_GET[’’]… var_dump($_GET[‘data’]) gives " array(1) { [“myDate”]=> string(0) “” } ". How do i make it as the input i get from DatePicker?

in action you have to declare a variable to recieve ur passed data

like


public function actionViewMessageBoardByDate($data)

    {  

                var_dump($data);

                $this->renderPartial('/message/viewMessageBoardOnly',array('data'=>$data));

        }   

or if POST method using ajax

then


echo $_POST['data'];

Tried adding variable $data to the action . Gives a Error 400 : request invalid.

commenting out the renderPartial, leaving only


var_dump($data);

inside the action also gives the error.

tried

var_dump($_GET[‘data’]).

Also the same.

Var_dump is invalid???

[EDIT]

above is output when clicking the normal button. Clicking the Ajax button gives no response.

Dear Friend

1.We can insert form tags below and above.

2.Use AjaxSubmitButton




<div class="form">

<?php 

echo CHtml::beginForm();

$this->widget('zii.widgets.jui.CJuiDatePicker', array( // using datepicker to get input date

                        'model' => $model,

                        'attribute' => 'start_date',


));

                 //$data["myDate"] = $model->start_date; 

                // var_dump($data);


                 

  echo CHtml::button('Click me', array('submit' => array('message/viewMessageBoardByDate')));


  echo CHtml::ajaxSubmitButton('Index submit button',

            array('message/viewMessageBoardByDate'), //controller 

            array('update'=>'#data')     

        );


echo CHtml::endForm();


?>

</div>

<div id="data"></div>



Then in your controller you can call the data.




if(isset($_POST["Message"])) //ASSUMING THAT $model IS INSTANCE OF CLASS Message.

	echo $_POST["Message"]["start_date"];



Regards.

Wow… so I didn’t insert form tags !

Now the Button works as i expected. Clicking it saves the input value from DatePicker, and passes it to action/viewMessageBoardByDate and which then renders the filtered result.yay!

But the Ajax Button still can’t see the input value, it gives NULL. @@

and ajaxSubmitButton Works !

Thank you !!! ^_^

can you point me to any material to understand the difference between ajaxButton vs ajaxSubmitButton? or in what case do people use them? They looks the same to me.(that thought cost me 2 days… haha)