Yii framework radiobuttonlist - php

Can anyone shine a light on how to determine which radio button has been selected when the form has been submitted?
I am using CActiveForm::radioButtonList?

You don't need to determine it. Client will transmit its value in POST data.
For example such code
<?=$form->radioButtonList($person,'gender_code',array('m'=>'Male','f'=>'Female')); ?>
will form POST[gender_code]=m or POST[gender_code]=f

Radio List Reflects simple form Submitting process. If you have following list implementation for example
<div class="form">
<?php echo CHtml::beginForm(); ?>
<div class="row">
<?php
echo CHtml::radioButtonList(
'registerMode',
'consumer',
array(
'consumer'=>'I am a FOODIE ',
'staff'=>'I want to give Services ',
),
array('template'=>'<div class="rb">{input}</div><div class="rb">{label}</div><div class="clear"> </div>')
);
?>
</div>
<div class="row">
<?php echo CHtml::submitButton('Register',array('class'=>'submit')); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
when submitted following input is generated
array
(
'registerMode' => 'consumer'
'yt0' => 'Register'
)
it represents name or Index of the option Selected
following code can get values
if(isset($_POST['registerMode']))
CVarDumper::Dump($_POST['registerMode'],100,true);
Good Luck

Related

How to get CHtml::dropDownList value exactly after option is chosen

everyone. I am new to PHP and Yii framework and I have a problem. I have php file, which I want to create depending on user choices.
The code below represents a dropdown list with driver names from my database:
<div class="row top10">
<div class="col-md-6 ">
<?php echo CHtml::dropDownList(
'driver-names',
$dropvalue,
(array)Driver::driverDropDownList(Driver::getUserId()),
array(
'class' => "form-control"
)
)
?>
</div>
</div>
After choosing a certain name from the dropwodn I want to know which name is chosen and depending on its value to show different html. Something like that:
<?php if($dropvalue=='George'):?>
<label class="col-sm-2 control-label"><?php echo Driver::t("first check") ?></label>
<div class="col-sm-6">
<?php
echo CHtml::checkBox(
'yes and no',
$dropvalue=='George' ? true : false,
array(
'class' => "switch-boostrap"
)
)
?>
</div>
<?php endif;?>
I read the documentation about Yii and thought that it is enough to save the value into $dropvalue and then check it in if statement. But nothing is happening like that and my checkBox does not appear. In my opinion the second parameter in dropdownlist should be a variable in which the selected value will be placed. So, I also tried $dropvalue->dropvalue, instead of only $dropvalue.

$_POST is not working while sending Chtml data from view to controller. Its showing an empty array

I am new in Yii framework. I am using Chtml form to send data from view to controller and want to print the result and insert it to database. But in my case its showing an empty array. I couldn't able to understand where I am doing wrong. So please help me regarding this.
in controller
public function actionTest1()
{
$obj = new Pad;
if(isset($_POST))
print_r($_POST);// to check the desired result
if(isset($_POST['Pad']))
{
$obj->attributes=$_POST['Pad'];
if($obj->save()) // insert the data
$this->redirect(array('view','id'=>$obj->id));
}
}
in view
<?php echo CHtml::beginForm('','post',array('id'=>'step1Form')); ?>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'Stop Pad after goal is reached'));
?>
</div>
<div class="currencyText padTop4 floatLeft">Stop Pad after goal is reached</div>
<div class="clearBoth"></div>
</div>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'No limites'));
?>
</div>
<div class="currencyText padTop4 floatLeft">No limites</div>
<div class="clearBoth"></div>
</div>
<div class="nextText">
<?php echo CHtml::link('Next >',array('pad/test1'));?>
</div>
<?php echo CHtml::endForm(); ?>
Please help. Thanks in advance!
You are not submitting your form and of course $_POST will be empty. Line below is just a link:
<?php echo CHtml::link('Next >',array('pad/test1'));?>
You must change it to :
<?php echo CHtml::submitButton('Next >');?>
And also add an action to your form.
Updating the question with the changes I have done. Its redirect me to pad/test1 showing array() as result.
in controller
public function actionTest1()
{
$obj = new Pad;
if(isset($_POST))
CVarDumper::dump($_POST);// to check the desired result
if(isset($_POST['Pad']))
{
$obj->attributes=$_POST['Pad'];
if($obj->save()) // insert the data
echo "=====";
}
}
in view
<?php echo CHtml::beginForm( '','post'); ?>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'Stop Pad after goal is reached'));
?>
</div>
<div class="currencyText padTop4 floatLeft">Stop Pad after goal is reached</div>
<div class="clearBoth"></div>
</div>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'No limites'));
?>
</div>
<div class="currencyText padTop4 floatLeft">No limites</div>
<div class="clearBoth"></div>
</div>
<div class="nextText">
<?php echo CHtml::button('Next >', array('submit' => array('pad/test1'))); ?>
</div>
<?php echo CHtml::endForm(); ?>
A common source of confusion among new Yii users is how the 'safe' validator works, how it works with other validators, and why it's necessary in the first place. This article means to clear up this confusion, as well as explain the notion of Massive Assignment.
http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/

Set Field Empty When Change Password in Yii

I want to set empty this field:
This is my code from _form.php
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
how to that, please teach me. Thanks all
You can explicitly set the value to be an empty string like this:
<?php echo $form->passwordField($model,'password',array(
'size'=>50,
'maxlength'=>50,
'value'=>'',
)); ?>
Where you are starting your form, you can add htmlOptions like this;
'htmlOptions' => array(
'autocomplete' => 'off'
)
This will disable autocomplete for the whole form. If you just want to do it for a single field, add it to the htmlOptions for that file instead.
This strange behaviour is happen for me too.
I fix it with javascript code to set the password field value to empty.
Yii code:
<div class="row">
<?php echo $form->labelEx($model,'newpassword'); ?>
<?php echo $form->passwordField($model,'newpassword',array('size'=>60,'maxlength'=>128,'value'=>'')); ?>
<?php echo $form->error($model,'newpassword'); ?>
</div>
Javascript code (I am using jQuery) :
<script>
$(document).ready(function()
{
$("#ChangePasswordForm_newpassword").val("");
});
</script>
echo CHtml::passwordField('User[password]','')

Passing combobox selected value to a dialog using ajaxlink

I' trying to pass the dropDownList selected value to this Dialog.
Any ideas on how I can do this?
I tried adding another parameter to the ajaxlink, using array('id'=>'showEventoDialog','tipoaux'=>$data["tipo"]), or only $data->tipo but can't seem to do what I want.
I'm also trying to get the value via $_GET from the Dialog form.
Here's my form and the Dialog link within the form
<?php echo $form->labelEx($model,'tipo'); ?>
<?php echo $form->dropDownList($model,'tipo',Lookup::items('Teste')); ?>
<?php echo $form->error($model,'tipo'); ?>
...
<?php echo $form->labelEx($model,'eventoid'); ?>
<div id="evento">
<?php echo $form->dropDownList($model,'eventoid',CHtml::listData(Evento::model()->findAll(),'id', 'designacao'),array('prompt'=>'Escolha','class'=>'required')); ?>
<?php echo CHtml::ajaxLink(Yii::t('evento','Novo Evento'),$this->createUrl('evento/addnewcom'),array(
'onclick'=>'$("#eventoDialog").dialog("open"); return false;',
'update'=>'#eventoDialog'
),array('id'=>'showEventoDialog'));?>
<div id="eventoDialog"></div>
</div>
Any ideas on how to do this?
Plus will the solution work with any other type of value, like textfield or something else on my form, so I can pass the values to dialogs BEFORE the parent form is submitted.
You can hook up some code to the open event of the dialog that will fire right before the dialog appears. In this code you can query the selected option and write it to the dialog:
<?php echo $form->labelEx($model,'eventoid'); ?>
<div id="evento">
<?php echo $form->dropDownList($model,'eventoid',CHtml::listData(Evento::model()->findAll(),'id', 'designacao'),array('prompt'=>'Escolha','class'=>'required')); ?>
<?php echo CHtml::ajaxLink(Yii::t('evento','Novo Evento'),$this->createUrl('evento/addnewcom'),array(
'onclick'=>'$("#eventoDialog").dialog({open: function(){ $("#selectedvalue").text($("#eventoid").val()); }}) .dialog("open"); return false;',
'update'=>'#eventoDialog'
),array('id'=>'showEventoDialog'));?>
<div id="eventoDialog">
<span>Selected value: </span><span id="selectedvalue" />
</div>
</div>

fill data to the database from comment form of post view in yii

i have already the comment form bellow my post view , but the problem is that it can not be store the data by comment form to the database which is bellow post view.
My code for comment form....
<h5>Add your Comment</h5>
<?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
<?php else: ?>
<?php $comment= new Comment();
$this->renderPartial('/comment/_form',array('model'=>$comment,
)); ?>
and my code of _form is
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'content'); ?>
<?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'content'); ?>
</div>
i want to store data from the comment form which is in the post view.
I'm not 100% sure what you are asking here, but I think you are asking how to save the data from the form submit? In you controller, in the action that renders the comment form, use:
$model = new Comment;
if(isset($_POST['Comment'])){
$model->attributes=$_POST['Comment'];
$model->save();
}
You should check out the gii tool (http://yiitutorials.net/easy/using-yiis-gii-tool), it will help you generate your models, forms and action to save the form data. Hope that answered your question, apologies if it didn't!
On the downloaded source code you can find blog demo, and it has great example howto use comments :D

Categories