Processing form generated with CHtml

Hi there,

Since I don’t want to (can’t) use model in generation of a form (explained here) and therefore since I can’t take advantages of using CForm, CActiveForm or Form Builder, I wrote a simple (test version yet) form with using CHtml:


echo(CHtml::beginForm());

echo(CHtml::label('Name', 'name'));

echo(CHtml::textField('name', 'test'));

echo(CHtml::submitButton('Send!'));

echo(CHtml::endForm());

How to process such forms?

I tried to use old-school, not-object-oriented, not yii-aware approach of looking what is inside $_POST array:


echo('name='.$_POST['name'].'<br /><br />');

But this failed with PHP error saying: "Undefined index: name".

What am I missing?

Both documentation (guide / cookbook) and forum post seems to be very focused on building forms with using models and I can’t find anything that would easily explain how to build and process forms generated with pure CHtml approach, like in above example.

I’ve noticed that I was getting “Undefined index: name” error, because I had error_reporting = E_ALL | E_STRICT set in php.ini. After reverting it to error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED all went smudgily. After reverting it back to what it was (most strict error reporting mode) I had to change above line to:


if (isset($_POST['name'])) echo('name='.$_POST['name'].'<br /><br />');

But then again, question in post title remains. Is using good, old $_POST array the only (the best) solution for processing forms generated with using CHtml?

Hi Trej…

I am using it all the time when I require some specific features on my forms (not those automated by Yii)

There are plenty of ways to do so but, why not using the good Yii approach with models but applying them to what we want? -My english s*ks… let me code… :)

-on the view:

echo CHtml::textField(‘FORMNAME[fieldname]’);

-on the controller:

if(isset($_POST[‘FORMNAME’]))

In this way I just need to check once for the variable and create objects for validation, save, update… just like CActiveRecord does…

Just dropping ideas… you know me

Hi Antonio,

:) It is explained in the post, to which you’ve already answered (here), why I don’t want to (can’t) use model in generation of a form.

Thanks! That means I’m using a good approach! :)

Hi, with this solution, to get any attribute use the name set in the form:


$_POST['FORMNAME']['attribute_name']

Thanks, i found a way!