I have a form, populated with data from the database, with a radio list button in each record. After ticking and submitting the form, only the last record is saved to the database, but I want all records to be saved. Here is my code: //the form code fetching/querying the database
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'class')->label('Class')->dropDownList(
Arrayhelper::map(Classes::find()->all(), 'class_name',
'class_name'),
[
'maxlength' => true,
'style'=>'width: 300px; height: 50px;',
'prompt'=>'select class',
'onchange'=>'
$.post( "index.php?
r=attendance/list&id='.'"+$(this).val(), function(data){
$("section#Attend").html(data);
});'
]); ?>
//the controller code fetching and sending the result to the form
public function actionList($id){
$model = new Attendance;
$form = new ActiveForm;
$countclass = Registrationinfo::find()
->where(['current_class' => $id])
->count();
$lga = Registrationinfo::find()
->where(['current_class' => $id])
->all();
if($countclass > 0){
foreach($lga as $data){ ?>
<div >
<div class="col-md-1"><?= ('1')?></div>
<div style="display: inline;"><?= $data['first_name'] ?></div>
<div style="display: inline; margin-left: 20px;"><?=
$data['surname'] ?></div>
<div style="display: inline; margin-left: 79px;"><?=
$data['gender'] ?></div>
<div style="display: inline; margin-left: 20px;"><?= $form-
>field($model, 'student_id')->textInput(['value'=>$data->reg_no,
'disabled'=>''])->label(false) ?></div>
<div style="display: inline; margin-left: 150px;"> <?= $form-
>field($model, 'status')
->radioList(array('PR' => 'Present', 'PM' =>'Permission', 'LT'
=>'Late', 'AB' =>'Absent'), array('class' => 'i-checks')); ?>
<hr style="color: blue;">
<?php
}
}
else{
echo "<div class=>'info'>Invalide Class name </div>";
}
}
//my actionCreate from the same controller
public function actionCreate()
{
$model = [new Attendance()];
//select date from datesetup where status is open
$date = DateSetup::findbysql('SELECT date from date_setup WHERE
status = "open"')->all();
if(count($date) > 0){
foreach ($date as $dates);
}else{
echo "No date As been opened yet";
}
//select Term from Termsetup where status is open
$terms = Termsetup::findbysql('SELECT term from termsetup WHERE
status = "open"')->all();
if(count($terms) > 0){
foreach ($terms as $term);
}else{
echo "No Term As been opened yet";
}
if ($model->load(Yii::$app->request->post()) && $model->validate())
{
//adding the database value retrive to the attandance form field
$model->term = $term->term;
$model->day = date('d', strtotime('$dates'));
$model->month = date('m', strtotime('$dates'));
$model->year = date('Y', strtotime('$dates'));
$model->date = date('Y-m-d h:m:s');
$model->user_id = "admin";
if ( isset( $_POST['student_id'] ) ) {
$model->student_id = $_POST['student_id'];
// validate, save or more..
}
else {
echo "reg_no not posted"; //return array
}
//$model->student_id = $data->student_id;
//$model->user_id = Yii::$app->user->identity->user_id;
if($model->save())
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
Declare your name attribute of the forms as array, for update / insert go over the every element of the form (use for each) and run insert / update statement for every element.