How To Insert Append <Div> Data Into Database

Hi all, I am doing append data for <div> by using javascript now. But, I am struggling on how to insert data for the fields that I have appended? Can anyone guide me on this?

I have a button to click and once I click, the content of the <div> will be appended. How am I going to insert the data into database that are filled into the appended field? Thanks in advance~




<script>

	var _counter = 0;

    function Add() {

    _counter++;

    var oClone = document.getElementById("template").cloneNode(true);

    oClone.id += (_counter + "");

    document.getElementById("add").appendChild(oClone);

    }</script>


	<div id="add">

	<div id="template">

	<fieldset style="margin-left:10px; margin-right:10px; background-color:#FFFFE0">

	<div style="margin-left:85px">

    <?php echo $form->labelEx($model,'day'); ?>

	<div style="margin-left:95px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'day', CHtml::listData(CourseType::model()->findAll(), 'day', 'day'), array('empty'=>'Please Select Day')); ?>

    </div></div></br>


	<div style="margin-left:48px">

    <?php echo $form->labelEx($model,'start'); ?>

	<div style="margin-left:130px; margin-top:-30px">

	<?php 

    $this->widget('ext.timepicker.timepicker', array(

    'model'=>$model,

    'name'=>'start',

    ));

    ?> </div></div></br>


	<div style="margin-left:48px">

    <?php echo $form->labelEx($model,'end'); ?>

	<div style="margin-left:130px; margin-top:-30px">

	<?php 

    $this->widget('ext.timepicker.timepicker', array(

    'model'=>$model,

    'name'=>'end',

    ));

    ?> </div></div>


    </div></div>   

	

	<div style="margin-left:20px">

	<?php echo CHtml::button('Add More Details', array('onclick' => 'Add()')); ?>

	</div>

    </div></div>



Not sure I understand what you want to do but you could probably define an extra attribute to hold your data then append it to your loaded model in the beforeValidate method

Hi, georaldc. Thanks for the reply. Let me explain to u one more time. Currently, I am having 3 fields to fill out. I wish to append these 3 fields again as I need to fill out more details. But, I do not know how to save that extra 3 fields of data into database since the 3 fields of data are appended. Maybe the image in the attachment can help you to understand my problem clearly.

5373

Untitled.png

Here’s my code




<script>

	var _counter = 0;

    function Add() {

    _counter++;

    var oClone = document.getElementById("template").cloneNode(true);

    oClone.id += (_counter + "");

    document.getElementById("add").appendChild(oClone);

    }</script>


	<div id="add">                     //the 3 fields are appended once I clicked the button

	<div id="template">

	<fieldset style="margin-left:10px; margin-right:10px; background-color:#FFFFE0">

	<div style="margin-left:85px">

    <?php echo $form->labelEx($model,'day'); ?>

	<div style="margin-left:95px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'day', CHtml::listData(CourseType::model()->findAll(), 'day', 'day'), array('empty'=>'Please Select Day')); ?>

    </div></div></br>


	<div style="margin-left:48px">

    <?php echo $form->labelEx($model,'start'); ?>

	<div style="margin-left:130px; margin-top:-30px">

	<?php 

    $this->widget('ext.timepicker.timepicker', array(

    'model'=>$model,

    'name'=>'start',

    ));

    ?> </div></div></br>


	<div style="margin-left:48px">

    <?php echo $form->labelEx($model,'end'); ?>

	<div style="margin-left:130px; margin-top:-30px">

	<?php 

    $this->widget('ext.timepicker.timepicker', array(

    'model'=>$model,

    'name'=>'end',

    ));

    ?> </div></div>


    </div></div>   

	

	<div style="margin-left:20px">

	<?php echo CHtml::button('Add More Details', array('onclick' => 'Add()')); ?>

	</div>

    </div></div>



Looks like you want to insert tabular data. The Yii docs has a quick article on that:

http://www.yiiframework.com/doc/guide/1.1/en/form.table

Ya, but i am wondering how to insert the data from appended field into database?? This is because the appended field is the extra field from the database. If I create the extra fields in my database, how to make sure that the appended field is the extra field in the database? Do you get what I mean?

Thank you.

How does your database table or the model for this look like?

My database look like this:

id | code | name | type | credit_hour | prerequisite | session | total_student | day | start | end |

The [color="#FF0000"]day, start and end[/color] fields will be appended if the user wish to add more details. So, I was wondering how to insert the appended of day,start and end fields into database? The user may append more than one time. Can you guide me? Thanks~