Where To Process The Data Sent From A Form In Yii Framework

Hi,

I’m a newbie of Yii framework and I have some problems of understanding the things related to Form. Please help me out. Thanks in advance.

My question is, given a form, we are supposed to specify "action" property in pure php language. We write the code in the method that "action" is specified to process data sent from the form. In yii standard, it seems no "action" property for a form.

So I wonder where yii send the data collected from a form to, and by what identifier?

[size=2]It will send (post/get) data to the specific [/size]controller/action[size=2] from where you had render page.[/size]

Thank you very much, Kiran. Does that mean the post/get data will be sent back to the action method which renders this view originally in controller?

Hi,

please see it…

[size="2"]Yes,[/size]

You can check it using following code in controller,

if(isset($_POST))

{

print_r([size="2"]$_POST[/size][size="2"]);[/size]

}

I see. It is very very helpful. Thank you!

Hi Kiran, thanks for your reply again. I want to dig it a bit deeper. I will truely appreciate if you can help me. I’m now working on other people’s code. They have a submit button which is coded as:

		<?php $this->widget('bootstrap.widgets.TbButton', array(


				'buttonType' => 'submit',


				'id' => 'completed-task',


				'label' => TaskModule::t('Success'),


				'type' => 'success',


				'size' => 'normal',


				'htmlOptions' => array('class' => 'input-block-level', 'name' => 'TaskForm[completed]', 'value' => '1'),


			)); ?>

I thought if I clicked this button, the form data will send back to its original controller. However, what I found is that it may not go in. I attached the relevant part of code in controller and view at below. Please help me.

View:

<div class="taskbar">

&lt;div class=&quot;container-fluid clearfix&quot;&gt;


	&lt;div class=&quot;row-fluid&quot;&gt;


		&lt;div class=&quot;span10&quot;&gt;


			&lt;div class=&quot;task-info&quot;&gt;


				&lt;h2 class=&quot;task-title&quot;&gt;


				&lt;?php echo &#036;task-&gt;title ?&gt;


				&lt;/h2&gt;


				&lt;?php echo &#036;task-&gt;description; ?&gt;





			


				&lt;?php &#036;form = &#036;this-&gt;beginWidget('bootstrap.widgets.TbActiveForm', array('id'=&gt;'task-form')); ?&gt;			


				&lt;?php echo &#036;form-&gt;label(&#036;task,'answer', array('style'=&gt;'display: inline; margin-right: 30px')); ?&gt;


				&lt;?php echo &#036;form-&gt;textField(&#036;task,'answer_tester', array('name'=&gt; 'TaskForm[answer]')) ?&gt;


		&lt;/div&gt;


		&lt;/div&gt;


		


		&lt;div class=&quot;span2&quot;&gt;	


		&lt;?php &#036;this-&gt;widget('bootstrap.widgets.TbButton', array(


				'buttonType' =&gt; 'submit',


				'id' =&gt; 'completed-task',


				'label' =&gt; TaskModule::t('Success'),


				'type' =&gt; 'success',


				'size' =&gt; 'normal',


				'htmlOptions' =&gt; array('class' =&gt; 'input-block-level', 'name' =&gt; 'TaskForm[completed]', 'value' =&gt; '1'),


			)); ?&gt;


			&lt;?php &#036;this-&gt;widget('bootstrap.widgets.TbButton', array(


				'buttonType' =&gt; 'submit',


				'id' =&gt; 'abandon-task',


				'label' =&gt; TaskModule::t('Abandon'),


				'type' =&gt; 'danger',


				'size' =&gt; 'normal',


				'htmlOptions' =&gt; array('class' =&gt; 'input-block-level', 'name' =&gt; 'TaskForm[abandon]', 'value' =&gt; '1'),


			)); ?&gt;


		&lt;/div&gt;


		&lt;?php


		echo CHtml::hiddenField('TaskForm[session_id]', '', array('id' =&gt; 'session-id-field'));


		echo CHtml::hiddenField('TaskForm[status]', '', array('id' =&gt; 'status-field'));


		&#036;this-&gt;endWidget(); 


		?&gt;


	&lt;/div&gt;


&lt;/div&gt;

Controller:

public function wizardProcessStep($event)

{


	// Load log from database, if any.


	if (&#036;event-&gt;sender-&gt;read('welcome') &#33;= null) {


		&#036;welcomeData = &#036;event-&gt;sender-&gt;read('welcome');


		if (isset(&#036;welcomeData['log_id']))


			&#036;this-&gt;_testLog = TestLog::model()-&gt;findByPk(&#036;welcomeData['log_id']);


	}





	if (strpos(&#036;event-&gt;step, 'task') &#33;== false) {


		&#036;idSplit = explode('_', &#036;event-&gt;step);


		&#036;taskNo = &#036;idSplit[1];


		&#036;task = Task::model()-&gt;findByPk(&#036;taskNo);


		


		if (isset(&#036;_POST['TaskForm'])) {					


			&#036;session = TaskSession::model()-&gt;findByPk(intval(&#036;_POST['TaskForm']['session_id']));


			// Consolidate all page duration into session.


			&#036;session-&gt;duration = Yii::app()-&gt;db-&gt;createCommand(&quot;SELECT SUM( TIME_TO_SEC(p.end_time) - TIME_TO_SEC(p.start_time) ) as `duration` FROM {{task_pages}} p WHERE p.session_id = :session&quot;)-&gt;bindValue(':session', &#036;session-&gt;getAttribute('id'), PDO::PARAM_INT)-&gt;queryScalar();





			// Check if last visited page is one of the success URLs.


			if (&#036;_POST['TaskForm']['status'] == 'completed') {


				if (&#036;session-&gt;withinSuccessUrl() &amp;&amp; &#036;session-&gt;withinTimeLimit() &amp;&amp; &#036;session-&gt;withinClickLimit()) {


					&#036;session-&gt;status = TaskSession::STATUS_COMPLETED;


				}


			} else {


				&#036;session-&gt;status = TaskSession::STATUS_ABANDONED;


			}





			&#036;session-&gt;save();


			&#036;event-&gt;sender-&gt;save(null);


			&#036;event-&gt;handled = true;


			return;


		}





		// Custom layout for tasks.


		&#036;this-&gt;layout = '//layouts/task';


		&#036;this-&gt;render('task', array('task' =&gt; &#036;task, 'test' =&gt; &#036;this-&gt;_testModel, 'log' =&gt; &#036;this-&gt;_testLog, 'saved'=&gt; &#036;saved));





	} else if (method_exists(&#036;this, 'step' . ucfirst(&#036;event-&gt;step))) {


		// Allow steps to be processed by the individual functions if it exists.


		&#036;methodName = 'step' . ucfirst(&#036;event-&gt;step);


		&#036;this-&gt;&#036;methodName(&#036;event);


	}


}

[size="2"]Just open page in browser and see view sourcr (Ctrl+u)[/size]

There you will find form tag from which you can judge Controller to which data posted.

Something like below,

[color="#881280"][font="monospace"][size="2"]<form [/size][/font][/color][font="monospace"][size="2"]enctype[/size][/font][color="#881280"][font="monospace"][size="2"]="[/size][/font][/color][font="monospace"][size="2"]multipart/form-data[/size][/font][color="#881280"][font="monospace"][size="2"]" [/size][/font][/color][font="monospace"][size="2"]id[/size][/font][color="#881280"][font="monospace"][size="2"]="[/size][/font][/color][font="monospace"][size="2"]product-form[/size][/font][color="#881280"][font="monospace"][size="2"]" [/size][/font][/color][font="monospace"][size="2"]action[/size][/font][color="#881280"][font="monospace"][size="2"]="[/size][/font][/color][font="monospace"][size="2"]/backend.php?r=product/create[/size][/font][color="#881280"][font="monospace"][size="2"]" [/size][/font][/color][font="monospace"][size="2"]method[/size][/font][color="#881280"][font="monospace"][size="2"]="[/size][/font][/color][font="monospace"][size="2"]post[/size][/font][color="#881280"][font="monospace"][size="2"]">[/size][/font][/color]

[color="#881280"] [/color]