I read the book "Yii", I can't understand how to look at the global _POST array receiving by controller?
VIEW
_form.php
<div class="row">
<?php
echo $form->labelEx($model,'category_id');
echo CHtml::DropDownList('Page[category_id]','', Category::allCategory(),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('subcategory/dynamicSubCategories'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#Page_subcategory_id', //selector to update
// 'data'=>array('category_id'=>'js:this.value'),
//leave out the data key to pass all form values through
)));
echo $form->error($model,'category_id');
?>
</div>
<div class="row">
<?php
echo $form->labelEx($model,'subcategory_id');
echo CHtml::dropDownList('Page[subcategory_id]','', array());
echo $form->error($model,'subcategory_id');
?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
SubcategoryController.php
public function actionDynamicSubCategories() {
$data = Subcategory::model()->findAllByAttributes(array('category_id' => $_POST['category_id']));
$data = CHtml::listData($data, 'id', 'title');
foreach ($data as $value => $name) {
echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
}
}
I'm interested in value from view echo CHtml::DropDownList('Page[category_id]','', Category::allCategory(), ... which comes to the controller SubcategoryController.php?
At the top of your controller you can just output the $_POST array.
e.g.
print_r($_POST);
or
var_dump($_POST);
This should show you any post values.
To get your _POST variable in Yii, you can use var_dump(Yii::app()->request->post);
In my situation into the controller passed this _POST (Page['category_id']).
I use in controller:
$x = Yii::app()->request->getPost('Page');
$y = $x['category_id'];
$data = Subcategory::model()->findAllByAttributes(array('category_id' => (int)$y));
Solved!
Related
Hello I have dropdownlist dependant textfield. The value of the textfield should be updated via ajax request when dropdownlist is selected (I'm using Yii's CHTML textfield).
Here is my code :
view _form.php
<div class="row">
<?php echo $form->labelEx($model,'kode_rincian'); ?>
<?php
echo $form->dropDownList($model, 'kode_rincian', array(), array(
'empty'=>'--Pilih--',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('OPS/calculateRealisasi'),
//'update'=>'#OPS_contrealisasi',
'dataType'=>'json',
'data'=>array(
'kode_program' => 'js:$(\'#OPS_kode_program option:selected\').val()',
'kode_kegiatan' => 'js:$(\'#OPS_kode_kegiatan option:selected\').val()',
'kode_output' => 'js:$(\'#OPS_kode_output option:selected\').val()',
'kode_komponen' => 'js:$(\'#OPS_kode_komponen option:selected\').val()',
'kode_akun' => 'js:$(\'#OPS_kode_akun option:selected\').val()',
'kode_rincian'=>'js:this.value',
),
'success'=>"function(data)
{
$('#OPS_realisasi_at').html(data.sumrealisasi);
$('#OPS_sisa_at').html(data.sumsisa);
} ",
)
));
?>
<?php echo $form->error($model,'kode_rincian'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'realisasi_at'); ?>
<?php
echo $form->textField($model,'realisasi_at');
?>
<?php echo $form->error($model,'realisasi_at'); ?>
</div class="row">
<?php echo $form->labelEx($model,'sisa_at'); ?>
<?php echo $form->textField($model,'sisa_at'); ?>
<?php echo $form->error($model,'sisa_at'); ?>
</div>
And controller OPSController.php:
public function actionCalculateRealisasi(){
$kode_rincian = $_POST["kode_rincian"];
$kode_program = $_POST["kode_program"];
$kode_kegiatan = $_POST["kode_kegiatan"];
$kode_komponen = $_POST["kode_komponen"];
$kode_akun = $_POST["kode_akun"];
$kode_output = $_POST["kode_output"];
$modelkode= KodePOK::model()->findByAttributes(array(
'kode_program'=>$kode_program,
'kode_komponen'=>$kode_komponen,
'kode_kegiatan'=>$kode_kegiatan,
'kode_akun'=>$kode_akun,
'kode_output'=>$kode_output,
'kode_rincian'=>$kode_rincian,
));
if($modelkode!=NULL){
$idpok=$modelkode->id_pok;
}
else{
$idpok=0;
}
$criteria = new CDbCriteria;
$criteria->select='SUM(jumlah_pengajuan) as realisasi';
$criteria->condition="id_pok='".$idpok."'";
$sum = OPS::model()->find($criteria);
$sumrealisasi=$sum->realisasi;
$sumrealisasi=(int)$sumrealisasi;
$calcsisa=POK::model()->findByPk($idpok);
$getjumlah=$calcsisa->jumlah_pagu;
$sumsisa=$getjumlah-$sumrealisasi;
echo CJSON::encode(array(
'sumrealisasi'=>$sumrealisasi,
'sumsisa'=>$sumsisa,
));
Yii::app()->end();
}
The code didn't show any error but the textfield wasn't updated. Please help me. Here is the result when I tried to inspect the page element :
I've solved the problem.
I changed
'success'=>"function(data)
{ $('#OPS_realisasi_at').html(data.sumrealisasi);
$('#OPS_sisa_at').html(data.sumsisa);
} ",
to :
'success'=>"function(data)
{ $('#OPS_realisasi_at').val(data.sumrealisasi);
$('#OPS_sisa_at').val(data.sumsisa);
} ",
I'm not sure how to describe my question. First I added a button at my CCGridview:
array(
'class'=>'CButtonColumn',
'template' => '{view}{update}{delete}{upload_image}',
'buttons' => array(
'upload_image' => array(
'label' => 'upload foto',
'url' => 'Yii::app()->createUrl("/image/create",
array("product_id" => $data->product_id))',
),
),
),
when I clicked it will bring me to the /image/create view which has a product_id value. For example on that gridview I clicked record number 7, so the url would be:
(webApp)/index.php/image/create?product_id=7
Since it rendering a partial _form so the form has the attribute according to the image table which has this attributes: id, title, filename, product_id.
So the view will be something like:
<div class="row">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textField($model,'title',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'filename'); ?>
<?php echo $form->fileField($model,'filename',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'filename'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'product_id'); ?>
<?php echo $form->textField($model,'product_id'); ?>
<?php echo $form->error($model,'product_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
My question is, how do we use the value from the url which I mentioned before is 7 (../create?product_id=7) into the product_id attribute without have to type it at provided textField?
In other word I will remove this from the view:
<div class="row">
<?php echo $form->labelEx($model,'product_id'); ?>
<?php echo $form->textField($model,'product_id'); ?>
<?php echo $form->error($model,'product_id'); ?>
</div>
But when i submitted, the form the value (7) should have been passed/saved at product_id field.
Added:
My controller actionCreate is
//...
public function actionCreate()
{
$dir = Yii::app()->basePath . '/../productimages/';
$uploaded = false;
$model=new Image();
if(isset($_POST['Image']))
{
$model->attributes=$_POST['Image'];
$tempSave=CUploadedFile::getInstance($model,'filename');
if($model->validate())
{
$uploaded = $tempSave->saveAs($dir.'/'.$tempSave->getName());
$this->redirect(array('/products/index'));
}
}
$this->render('index', array(
'model' => $model,
'uploaded' => $uploaded,
'dir' => $dir,
));
}
That's it. Many thanks..
modify your controller this way:
if(isset($_POST['Image']))
{
$model->attributes=$_POST['Image'];
$tempSave=CUploadedFile::getInstance($model,'filename');
if($model->validate())
{
$uploaded = $tempSave->saveAs($dir.'/'.$tempSave->getName());
$this->redirect(array('/products/index'));
}
} else {
//initialize defaults
$model->product_id=intval($_GET['product_id']);
}
I have view:
...
<div class="row">
<?php echo $form->labelEx($model,'content'); ?>
<?php $this->widget('application.extensions.ckeditor.CKEditor',array(
'model'=>$model,
'attribute'=>'content',
'language'=>'en',
'editorTemplate'=>'full',));
?>
<?php echo $form->error($model,'content'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'status'); ?>
<?php echo $form->dropDownList($model,'status', array('0' => 'Not published', '1' => 'Published')); ?>
<?php echo $form->error($model,'status'); ?>
</div>
<div class="row">
<?php
echo $form->labelEx($model,'category_id');
echo CHtml::dropDownList('category_id','', Category::allCategory(),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('subcategory/dynamicSubCategories'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#subcategory_id', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
)));
echo $form->error($model,'category_id');
?>
</div>
<div class="row">
<?php
echo $form->labelEx($model,'subcategory_id');
echo CHtml::dropDownList('subcategory_id','', array());
echo $form->error($model,'subcategory_id');
?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
...
Data from the fields 'category_id' and 'subcategory_id' don't write in a database. All other fields successfully register. Prompt as me to tie this field to a form and to solve the matter?
Use this method for dropDownLists
echo CHtml::activeDropDownList($model, 'category_id',Category::allCategory()
....
Or better use form method
echo $form->dropDownList($model, 'category_id', Category::allCategory()
....
this is because the data that is saved, should be part of the models array data, so change your new field name to reflect MODELNAME[ATTRIBUTENAME]
echo CHtml::dropDownList('MODELNAME[category_id]', ...
or simply change to activeDropdownList
i am creating yii application first time, i have used following code to create dropdownlist in my view
echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll());
it show me error
include(Forms.php): failed to open stream: No such file or directory
C:\wamp\www\LearningYii\protected\views\codelookup_form.php(35):
<?php echo $form->textField($model,'ShortDesc',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'ShortDesc'); ?>
</div>
<div class="row">
<?php echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll()); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
$opts = CHtml::listData(Codelookup::model()->findAll(),'CodeLookupId','CodeDesc');
echo $form->dropDownList($model,'ParentCodeLookupId',$opts,array('empty'=>''));
This code is working
use CHtml::listData to convert the db object to list data:
Here ID , NAME is your table values which should be used in option value and option text.
<?php
$opts = CHtml::listData(Forms::model()->findAll(),'CodeLookupId','CodeLookupId');
//to check list
print_r($opts);
echo $form->dropDownList($model,'CodeLookupId',$opts);
?>
try hope this will help..
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php
$records = loaddataform::model()->findAll();
$list = CHtml::listData($records, 'id', 'username');
echo CHtml::dropDownList('loaddataform', $model,
CHtml::listData($records,
'a_id', 'a_name'),
array('empty' => '(Select a name)'));
?>
</div>
and in controller
public function actionloaddata()
{
$model = new loaddataform;
$models = loaddataform::model()->findAll(array('order' => 'a_name'));
$list = CHtml::listData($models, 'a_id', 'a_name');
$this->render('loaddata',array('model'=>$model));
}
This works correctly..it retrives name from database.
I am doing a yii web application
i have a drop down list that should be dependent on another , i use ajax however it doesnt work.
ive seen the yii tutorial for dependent drop downs and searched everywhere.
http://www.yiiframework.com/wiki/24
this is my main drop down list:
<div class="row">
<?php echo $form->labelEx($model, 'sourceID'); ?>
<?php
echo $form->dropDownList($model, 'sourceID', CHtml::listData(Sources::model()->findAll(), 'sourceID', 'name'), array('empty' => 'select source'), array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('reservations/atest'),
'update' => '#meal'
)
)
);
?>
<?php echo $form->error($model, 'sourceID'); ?>
</div>
this is the dependent drop down list :
<div class="row">
<?php echo $form->labelEx($model, 'meal'); ?>
<?php echo $form->dropDownList($model, 'meal', array()); ?>
<?php echo $form->error($model, 'meal'); ?>
</div>
this is my controller action:
public function actionAtest() {
$data = Sources::model()->findAll();
$data = CHtml::listData($data, 'sourceID', 'name');
foreach ($data as $value => $name) {
echo CHtml::tag('option', array('value' => $value), CHtml::encode($name),true);
} }
also, i added the action to the access rules.
any help is appreciated ,
thank you in advance.
You placed the ajax option after the htmlOptions. Here is the modified code
<div class="row">
<?php echo $form->labelEx($model, 'sourceID'); ?>
<?php
echo $form->dropDownList($model, 'sourceID', CHtml::listData(Sources::model()->findAll(), 'sourceID', 'name'), array('empty' => 'select source','ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('reservations/atest'),
'update' => '#meal'
)
)
);
?>
<?php echo $form->error($model, 'sourceID'); ?>
</div>
And instead of using forms dropdownlist use CHtml::dropDownList for the dependent drop-down.
echo CHtml::dropDownList('meal','', array());
You can also use CActiveForm::dropDownList but in that case you have to use CHtml::resolveNameId in the update option of ajax