I am trying to create a Quiz app, with many MCQ questions that are stored in Database. I successfully get questions and options from Db and display it.
However, when I try to post the checked values to Db it doesn't work.
I am using ActiveForm and RadioList inside of it.
All data is taken from Db using for loop.
Also, I need to pass the question_id when posting the checked value.
I have Tables:
Questions - questions table.
Answers - table where I want to write the checked values.
Options - Table from where I get the options(a,b,c) for the questions.
Answers Controller:
public function actionSave()
{
$model = new Answers;
$request = \Yii::$app->getRequest();
if ($request->isPost && $model->load($request->post())) {
\Yii::$app->response->format = Response::FORMAT_JSON;
return ['success' => $model->save()];
}
return $this->renderAjax('index', [
'model' => $model,
]);
}
public function actionCreate()
{
$model = new Answers;
$this->save($model);
}
protected function save($model)
{
if (isset($_POST['Answers'])) {
$model->attributes = $_POST['Answers'];
if ($model->save()) {
$this->redirect(array('index'));
}
}
$this->render('_form', compact('model'));
}
Here is the view:
<?php $form = ActiveForm::begin(); ?>
<?php for ($i=0; $i<count($questions); $i++): ?>
<div>
<?= Html::encode("{$questions[$i]->title}") ?>
</div>
<?php $options = Options::find()->where(['question_id'=>$questions[$i]->id])->orderBy('id ASC')->all();
$options = ArrayHelper::map($options,'id', 'title');?>
<label class="container" >
<?= $form->field($model, 'option_id')->radioList($options, ['name'=>'Questions['.$questions[$i]->id.']', 'separator' => '<br>' ])->label(false) ?>
</label>
<?php endfor; ?>
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
I expect that when user answers all questions and press the "Save" button, it will save the checked value and question_id to the table in Db. However, now it do nothing, and do not show any errors.
I am working on this first time, so please can you help me to understand how to do it?
We are lacking the code of the model, as it is right now you could do:
Dump the post inside the action with var_dump($request->post());die();
Check if the values are on the post.
Check if the field name are in the rules of the class.
Check if the name of the field in the post is right.
Related
I have a survey app with many questions. Each question has options that are presented in form of radiolist.
I am using the ActiveForm and RadioList in for loop in order to get all the questions and options from the Database.
Everything is ok with printing the questions and options but
When I try to save the answers to the database, it saves only the last option.
In my save action I tried to put a foreach loop in order to save each answer, but it didn't work for me.
I tried to var_dump the $model->save and $request->post('Questions') there is all the selected options, not only the last one.
Model:
here is only the rules:
public function rules(){
return[
[['id','question_id', 'option_id'], 'required']
];
}
View:
<?php $form = ActiveForm::begin([
'id' => 'my-form-id',
'action' => ['answers/save'],
]
);
?>
<?php $questions = Questions::find()->orderBy('id ASC')->all(); ?>
<?php for ($i=0; $i<count($questions); $i++): ?>
<?= Html::encode("{$questions[$i]->title}") ?>
<?php $options = Options::find()->where (['question_id'=>$questions[$i]->id])->orderBy('id ASC')->all();
$options = ArrayHelper::map($options,'id', 'title');
?>
<label class="container" >
<?= $form->field($model, 'option_title')->radioList(
$options,
['name'=>'Questions['.$questions[$i]->id.']',
'separator' => '<br>',
])->label(false) ?>
</label>
<?php endfor; ?>
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
Controller:
public function actionSave(){
$model = new Answers();
$request = \Yii::$app->request;
foreach($request->post('Questions') as $key=>$value) {
$model->load($request->post());
$model->option_id = $value;
$model->question_id = $key;
$model->save();
}
}
Sorry guys if it is obvious question but I really do not understand how to do it. Googling also didn't helped.
If you have any ideas please share
You need to move the $model = new Answers(); inside the loop as you need to save all the checkboxes by looping on the post array you should create a new object every time and then it will save all of them. Just change your code to the below
public function actionSave(){
$request = \Yii::$app->request;
foreach($request->post('Questions') as $key=>$value) {
$model = new Answers();
$model->load($request->post());
$model->option_id = $value;
$model->question_id = $key;
$model->save();
}
}
Also you should use transaction block when working with related or multiple records like in this case you should either save all of them or none in case of any error or exception, currently it isnt the case. If the exception or error occurs on the 4th checkbox you still have the first 3 checkbox values saved. Try wrapping the code like below
public function actionSave(){
$request = \Yii::$app->request;
//start transaction
$transaction=Yii::$app->db->beginTransaction();
try{
foreach ($request->post('Questions') as $key => $value) {
$model = new Answers();
$model->load($request->post());
$model->option_id = $value;
$model->question_id = $key;
$model->save();
}
//commit the transaction to save the records
$transaction->commit();
}catch(\Exception $e){
//rollback the transaction so none of the checkboxes are saved
$transaction->rollBack();
//do your stuff intimate the user by adding the message to a flash and redirecting
}
}
The Problem: If I select the new checkbox, it will update the data, but when I unchecked all the existing checkbox, it is not working. Even when I unchecked multiple checkboxes and leave one, it still works. Just not working when I select all unchecked checkbox to update data. Any suggestion would be greatly appreciated
This is my model that I am using get join table ID:
public function getProfileCampaigns($campaignIds = true) {
$campaignData = array();
$campaignProfiles = Yii::app()->db->createCommand()->select('campaign_id')
->from('campaign_profiles')
->where('profile_id = :profile_id',array(':profile_id' => $this->profile_id))
->queryAll();
// Check if need to send only campaign ids
if ($campaignIds) {
foreach ($campaignProfiles as $campaignProfile) {
$campaignData[] = $campaignProfile['campaign_id'];
}
}
return $campaignData;
}
This my controller for update action:
public function actionUpdate($id)
{
$model = $this->loadModel($id);
$model->setScenario(Profile::SCENARIO_UPDATE);
// Get active campaigns
$campaigns = Campaign::model()->findAll();
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
// Check if profile have any releated profile
$model->campaignIds = $model->getProfileCampaigns();
if(isset($_POST['Profile']))
{
$model->attributes=$_POST['Profile'];
if($model->validate()) {
$model->save();
// Check if any campaign choosed
if ($_POST['Profile']['campaignIds']) {
Yii::app()->db->createCommand()->delete('campaign_profiles', 'profile_id = :profile_id', array(':profile_id' => $model->profile_id));
foreach ($_POST['Profile']['campaignIds'] as $campaignId) {
$campaignProfile = new CampaignProfile();
$campaignProfile->setIsNewRecord(true);
$campaignProfile->campaign_id = $campaignId;
$campaignProfile->profile_id = $model->profile_id;
$campaignProfile->save();
}
Yii::app()->user->setFlash('success', 'The Profile was successfully updated.');
$this->redirect(array('update','id'=>$model->profile_id));
}
}
}
$this->render('update',array(
'model' => $model,
'campaignListData' =>$campaigns,
));
}
This is form for getting checkbox select for update:
<div class="form-group">
<?php echo $form->labelEx($model,'campaignIds'); ?>
<div class="col-sm-9">
<?php echo $form->checkBoxList($model, 'campaignIds', CHtml::listData($campaignListData, 'id', 'name')); ?>
<?php echo $form->error($model,'campaignIds'); ?>
</div>
</div>
That is because only selected checboxes are send as form data. When no checkbox is checked, no data is sent, so probably default/old value from model is used.
You may use uncheckValue setting to define default value which will be send, when no checkbox is checked:
<div class="form-group">
<?php echo $form->labelEx($model,'campaignIds'); ?>
<div class="col-sm-9">
<?php echo $form->checkBoxList(
$model,
'campaignIds',
CHtml::listData($campaignListData, 'id', 'name'),
['uncheckValue' => '']
); ?>
<?php echo $form->error($model,'campaignIds'); ?>
</div>
</div>
Since 1.1.7, a special option named 'uncheckValue' is available. It can be used to set the value that will be returned when the checkbox is not checked. By default, this value is ''. Internally, a hidden field is rendered so when the checkbox is not checked, we can still obtain the value. If 'uncheckValue' is set to NULL, there will be no hidden field rendered.
https://www.yiiframework.com/doc/api/1.1/CHtml#activeCheckBoxList-detail
I am newbie to yii2. I am trying to create my simple form in yii2 to retrieve password. Here is class code:
<?php
namespace app\models;
use yii\base\Model;
class RetrievePasswordForm extends Model
{
public $email;
public function rules()
{
return [
['email', 'required'],
['email', 'email'],
];
}
}
Here is action code:
$model = new RetrievePasswordForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()){
return $this->render('retrievepassword-confirm', ['model' => $model]);
} else {
return $this->render('retrievepassword', ['model' => $model]);
}
My form looks like this:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$this->title = 'Retrieve password';
$this->params['breadcrumbs'][] = $this->title;
?>
<h1><?= Html::encode($this->title) ?></h1>
<p>We will send link to retrieve your password to the following email:</p>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'email')->textInput(['style'=>'width:200px'])?>
<div class="form-group">
<?= Html::submitButton('Send', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
The problem is that $model->load(Yii::$app->request->post()) always returns false, so when I am clicking "submit" button, page just reloads.
I am currently working without database. I am just creating form and trying to go to another form, when valid data received in model. Thanks for help.
Try explicitally assign the method and the action to the active Form
Then Assuming that your target action is named actionRetrivePassword
<?php $form = ActiveForm::begin([
'method' => 'post',
'action' => Url::to(['/site/retrivepassword']
); ?>
I'll go with I feel it's wrong, if this doesn't help, please give more information.
It's a registration form, so I assume you need email and password for that (since you have those columns in database). But you also declared public member $email in your model. This removes any value associated to $email from database. Therefore, remove this line:
public $email;
I Have my model with 2 fields Product.php:
[['ID_PRODUCT'], 'integer'],
[['NAME_PRODUCT'], 'string'],
my Controller ProductController.php:
public function actionCreate()
{
$model = new Product();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->ID_PRODUCT]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
And i want insert many times the same table with ActiveForm:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'ID_PRODUCT')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'NAME_PRODUCT')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ID_PRODUCT')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'NAME_PRODUCT')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
But when i save the information the fields are overwritten and only the last record is inserted
What you are trying to do is collect, validate and save tabular data. The reason it doesn't work is that in the form, Yii generates a name tag based on the field name and model, e.g. name="[Product]["ID_PRODUCT"]. When the form is sent to the server, the first fields get overwritten by the last ones, as they have the same name. The correct way to collect tabular input in a form is to add brackets at the end of the name, like this; name="[1][Product]["ID_PRODUCT"].Using this method, Yii gives ways of loading and validating multiple models.
Modify your controller code to use multiple models;
<?php
namespace app\controllers;
use Yii;
use yii\base\Model;
use yii\web\Controller;
use app\models\Product;
class ProductController extends Controller
{
public function actionCreate(){
//Find out how many products have been submitted by the form
$count = count(Yii::$app->request->post('Product', []));
//Send at least one model to the form
$products = [new Product()];
//Create an array of the products submitted
for($i = 1; $i < $count; $i++) {
$products[] = new Product();
}
//Load and validate the multiple models
if (Model::loadMultiple($products, Yii::$app->request->post()) && Model::validateMultiple($products)) {
foreach ($products as $product) {
//Try to save the models. Validation is not needed as it's already been done.
$product->save(false);
}
return $this->redirect('view');
}
return $this->render('create', ['products' => $products]);
}
}
Now you have all the data you need to populate the form, including any error messages generated for individual instances of you product model. The view file for the form needs to be altered like this, to use the multiple models;
foreach ($products as $index => $product) {
echo $form->field($product, "[$index]ID_PRODUCT")->label($product->ID_PRODUCT);
echo $form->field($product, "[$index]NAME_PRODUCT")->label($product->NAME_PRODUCT);
}
All of this is covered in the Yii2 documentation
value of FORM INPUT Help!!
//this is just a refrence of $nm and $fid from test_model//
$data['fid']['value'] = 0;
$data['nm'] = array('name'=>'fname',
'id'=>'id');
say i have one form_view with
<?=form_label('Insert Your Name :')?>
<?=form_input($nm)?>
and a function to get single row
function get($id){
$query = $this->db->getwhere('test',array('id'=>$id));
return $query->row_array();
}
then in controller.. index($id = 0)
and somewhere in index
if((int)$id > 0)
{
$q = $this->test_model->get($id);
$data['fid']['value'] = $q['id'];
$data['nm']['value'] = $q['name'];
}
and mysql table has something like 1. victor, 2. visible etc. as a name value
but here its not taking the value of name and id from form_input and not showing it again in form_view in same input box as victor etc so to update and post it back to database...
anyone please help!!
and please be easy as I am new to CI!!
Based on your comment to my first answer, here is a sample of a Controller, Model and View to update a user entry pulled from a table in a database.
Controller
class Users extends Controller
{
function Users()
{
parent::Controller();
}
function browse()
{
}
function edit($id)
{
// Fetch user by id
$user = $this->user_model->get_user($id);
// Form validation
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
if ($this->form_validation->run())
{
// Update user
$user['name'] = $this->input->post('name', true);
$this->user_model->update_user($user);
// Redirect to some other page
redirect('users/browse');
}
else
{
// Load edit view
$this->load->view('users/edit', array('user' => $user));
}
}
}
Model
class User_model extends Model
{
function User_model()
{
parent::Model();
}
function get_user($user_id)
{
$sql = 'select * from users where user_id=?';
$query = $this->db->query($sql, array($user_id));
return $query->row();
}
function update_user($user)
{
$this->db->where(array('user_id' => $user['user_id']));
$this->db->update('users', $user);
}
}
View
<?php echo form_open('users/edit/' . $user['user_id']); ?>
<div>
<label for="name">Name:</label>
<input type="text" name="name" value="<?php echo set_value('name', $user['name']); ?>" />
</div>
<div>
<input type="submit" value="Update" />
</div>
<?php echo form_close(); ?>
It's hard to see the problem from your snippets of code, please try and give a little more information as to the structure of your app and where these code samples are placed.
Presume in the last code listing ('somewhere in index') you are getting $id from the form, but you define the ID of the form input box as 'id' array('name'=>'fname','id'=>'id') rather than an integer value so maybe this is where the problem lies.
Where does the $data array get passed to in the third code listing?
From your question I think you want to display a form to edit a person record in the database.
Controller code
// Normally data object is retrieved from the database
// This is just to simplify the code
$person = array('id' => 1, 'name' => 'stephenc');
// Pass to the view
$this->load->view('my_view_name', array('person' => $person));
View code
<?php echo form_label('Your name: ', 'name'); ?>
<?php echo form_input(array('name' => 'name', 'value' => $person['name'])); ?>
Don't forget to echo what is returned from form_label and form_input. This could be where you are going wrong.