[Solved] Add form fields with js


I have a problem with form submitting in Yii after adding additional fields with jQuery.

So, I have piece of JavaScript in my _form view, and adding elements works fine. But upon submitting form with additional fields added by jQuery, I don’t get those values in $_POST array.



	<div class="note-wrap">

		<h3><?php echo CHtml::label(Yii::t('strings', 'Important'), false); ?></h3>

		<?php foreach($model->notes as $note): ?>

			<div class="row">

				<?php echo CHtml::textArea('note['.$note->id.']', $note->content); ?>


		<?php endforeach; ?>

	</div><!-- note-wrap end -->

	<?php echo CHtml::link(Yii::t('strings', 'Add note'), '#', array('id'=>'add-note')); ?>


<?php Yii::app()->clientScript->registerScript('jquery-add-note', "


$('#add-note').click(function(e) {


	c +=1;

	$('.note-wrap').append('<textarea id=\"note_'+c+'\" name=\"note[]\"></textarea>');


", CClientScript::POS_READY); ?>

Anyone know what seems to be the problem here?

Thank you! :)

do the new forms have proper names? May be


1 Like

Yes, they have. If I add additional fields in _form view manually then I get their values properly, but if I add the same fields with js, then I can’t get values when submitting form.

Weird thing is that it works perfectly in simple forms written in php, something like this.


What Firebug (or other JS console) says? Do you see JS generated <textarea>-s?

Also look at this because you didn’t explicitly set name[KEY]:

This of course isn’t reason why elements aren’t posted, but just in case you get unwanted results.

I think I figured this out. Markup was messed up. I’ve had something like this:

<div class="form">

    <form ...>

        ... form fields ...

</div><!-- end of form div -->

<div class="sidebar">

        ... more form fields ... -->


</div><!-- end of sidebar div -->


I never thought that something like this can affect form functioning. Anyway, thank you! :)