<Form Action="test.php"> Into Yii

I have been pondering about forms, and one thing I’ve observed in Yii is that all forms are to be saved in a model. But i only want my data to be saved in certain variables only, not in a database.

I’m making a chart form:




<h1>Graph a Chart</h1>

<form action="chart1.php" method="POST">

<?php

echo "<div>";

	echo "<h4>Choose a crop: &nbsp &nbsp &nbsp";

	$list=CHtml::listData(Crop::model()->findAll(array('order' => 'crop_name ASC')), 'crop_id', 'crop_name');

	$list2=CHtml::listData(Crop::model()->findAll(array('order' => 'crop_name ASC')), 'crop_id', 'crop_id');

	?>

	<select name= "crop_id"> 

	<?php

	$y=0;

	$crop_num = array();

	foreach($list2 as $l)

	{

		$crop_num[$y] = $l;

		$y++;

	}

	$z=0;

	foreach($list as $c)

	{

		echo "<option value=$crop_num[$z]>".ucfirst($c)."</option>";

		$z++;

	}

	

	?></h4>

	</select>

	<?php

echo "</div>";

echo "<div>";

echo "<font color='red'>WARNING: Starting date must not exceed End date!</font>";

echo "</div>";

echo "<div>";

	echo "<h4>Initial date to graph: ";

	echo "&nbsp &nbsp &nbsp <input type='date' name='s_date'></h4>";

echo "</div>";

echo "<div>";

	echo "<h4>Last date to graph:";

	echo "&nbsp &nbsp &nbsp <input type='date' name='e_date'></h4>";

echo "</div>";

	

	echo "<input type='submit'>";

?>


</form>




This code is a form that asks the crop, start and end date. Then, when submit comes into play, I would transfer into chart1.php, but problem is, it is out of the /protected folder. It is located in localhost/webapp/chart1.php. How could I do this in Yii?

In chart1.php by the way I would like to echo first the $_POST variables I have gotten in chart.php

But you can still use a CFormModel. CFormModel doesn’t have to be a container of a DB related data.

And by using CFormModel (or CActiveRecord) for your form, you can significantly simplify the view script and the controller code. And also you can implement a nice validation feature to your form very easily.

I wonder why you locate chart1.php outside of the /protected folder. Is it completely an independent PHP script without any interaction with Yii?

If so, you can specify the action of the form in the absolute path.




<form action="/chart1.php" method="POST">



Or




<?php echo CHtml::form('/chart1.php'); ?>



I have this file in localhost/webapp/protected/views/price/chart.php

<form action="/chart1.php" method="POST">

but when submitted it will redirect to localhost/webapp/chart1.php,

how can I do that it will be directed to

localhost/webapp/protected/views/price/chart1.php?

If i change action="/chart1.php" into action="/protected/views/price/chart1.php"

it will say that I don’t have enough access.

Ah, OK …

You should not directly call char1.php, because it’s a view script. It should be rendered in some controller action using CController::render().




<?php echo CHtml::form(array('chart1')); ?>



The above should call ‘http://localhost/webapp/price/chart1’ or something like that.

And you should handle this request in your PriceController::actionChart1() method, just as you are handling ‘http://localhost/webapp/price/chart’ with your PriceController::actionChart() method.

And in your actionChart1 method, you will render chart1.php by calling CController::render().




public function actionChart1()

{

	...

	$this->render('chart1',array(

		...

	));

}



And, I’m afraid you are not accustomed to the development using Yii framework (including the MVC design pattern).

I’d like to advise you to finish the blog tutorial (or Larry Ullman’s tutorial) before you try to build your own app. :)

This line of code was the only one I couldn’t figure out, the calling of an action using a form. But the MVC, i’ve already read of it. :) I know some concepts, but structures of the codes in Yii, i’ve never know of. :)

Thanks for the help :)

I think you could combine your "chart" and "chart1" actions, and share the form and view.

You can use some Gii-generated “create” action as a template (or a skeleton) for it. Just replace the CActiveRecord model with a CFormModel. :)