Yii Framework Forum: Passing Variable Thru Buttons To Controller Not Working - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Passing Variable Thru Buttons To Controller Not Working Rate Topic: -----

#1 User is offline   Chew JW 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 21-March 13

Posted 22 March 2013 - 02:44 AM

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?

This post has been edited by Chew JW: 22 March 2013 - 02:58 AM

0

#2 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 22 March 2013 - 02:50 AM

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'];


Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#3 User is offline   Chew JW 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 21-March 13

Posted 22 March 2013 - 03:12 AM

View PostLal Zada, on 22 March 2013 - 02:50 AM, said:

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.
0

#4 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 22 March 2013 - 04:08 AM

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.
0

#5 User is offline   Chew JW 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 21-March 13

Posted 22 March 2013 - 04:31 AM

View Postseenivasan, on 22 March 2013 - 04:08 AM, said:

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)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users