I have three buttons which open 1 modal window on a page:
<?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'myModal',)); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4 class="text-center">Pay</h4>
</div>
<div class="modal-body">
<?php
$this->widget('bootstrap.widgets.TbDetailView', array(
'type'=>'bordered condensed',
'data'=>$model,
'attributes'=>array(
array(
'type'=>'raw',
'label'=>'Период',
'value'=> '', // if (clicked button id is 1 value == 1 day) else if (clicked button id is 2 value == 7 days)
),
),
));
?>
</div>
<div class="modal-footer">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'type'=>'primary',
'label'=>'Проверить',
'url'=>'#',
'htmlOptions'=>array('data-dismiss'=>'modal'),
)); ?>
</div>
<?php $this->endWidget(); ?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
'encodeLabel' => false,
'label'=>'1 day',
'type'=>'primary',
'htmlOptions'=>array(
'id' => '1',
'onclick' => 'alert(this.id)',
'data-toggle'=>'modal',
'data-target'=>'#myModal',
),
)); ?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
'encodeLabel' => false,
'label'=>'7 dayss',
'type'=>'primary',
'htmlOptions'=>array(
'id' => '2',
'onclick' => 'alert(this.id)',
'data-toggle'=>'modal',
'data-target'=>'#myModal',
),
)); ?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
'encodeLabel' => false,
'label'=>'1 month',
'type'=>'primary',
'htmlOptions'=>array(
'id' => '3',
'data-toggle'=>'modal',
'data-target'=>'#myModal',
),
)); ?>
How to pass the clicked button id to TbButton value?
if (clicked button id is 1 value == 1 day) else if (clicked button id is 2 value == 7 days) ...
First of all, id of elements on website need to be unique, you may want to consider using data attributes or calling those id differently.
Next thing is that only button that is submitting form is passing value with form to the server so you either need to use hidden input field or submit form by pressing that button or submit form using ajax request and attach it manually.
As I see you have put onclick event directly in element, better solution would be to put it outside in separate script tag/script file, but it is outside of your question.
What you need to do to assign value to be sent with form is to use following js to:
Assign code to input field:
$('#length_field').val(this.id)
or without jQuery:
document.getElementById('length_field').value = this.id
if you will use data-time-length="7" instead of id then you can do it by:
$('#length_field').val($(this).data('time-length'))
However it is unclear to me where you want to pass this value, what you want to do with it.
Related
<?php echo form_submit(['type'=>'submit', 'class'=>'btn login-btn', 'name'=>'btnLogin', 'i class'=>'icon-long-arrow-right' , 'value'=>'Login']); ?>
How to display an icon on button in Codeigniter.I tried this but it didn't show icon on button.Where to make change so that icon will displayed on button?
$data = array(
'name' => 'button',
'id' => 'button',
'value' => 'true',
'type' => 'submit',
'class'=>'btn btn-primary btn-sm icon-cancel-circle2',
'content' => '<i class="icon-long-arrow-right"></i>'
);
echo form_button($data);
you can use this.Hope it helps.
try this
<?php echo form_submit('btnLogin', 'Login', '"class"="btn login-btn" "i class"="icon-long-arrow-right"'); ?
the first parameter is the name, second is the value of your button.
I am trying to add new functionality to Storelocator module. I
have added new checkbox in admin and create new column in database
table. Saving checkbox value to databse is working. Now I want to print "recommended" value, but nothing happens. Why?
Checkbox field /Sparx/Storelocator/Block/Adminhtml/Storelocator/Edit/Tab/Form.php
$fieldset->addField('recommended', 'checkbox', array(
'label' => Mage::helper('storelocator')->__('Recommended'),
'onclick' => 'this.value = this.checked ? 1 : 0;',
'required' => false,
'name' => 'recommended',
'value' => '0',
));
Printing value /Sparx/Storelocator/controllers/IndexController.php
<div class="list-content">
<div class="loc-name">{{name}}</div>
<div>{{recommended}}</div>
<div class="loc-addr">{{address}}</div>
<div class="loc-addr3">{{city}}, {{state}} {{country}} {{postal}}</div>
<div class="loc-phone">{{phone}}</div>
<div class="loc-web">{{web}}</div>
{{#if distance}}<div class="loc-dist">{{distance}} {{length}}</div>
<div class="loc-directions">Directions</div>{{/if}}
{{#if storeid}}<div class="loc-desc">Store Details</div>{{/if}}
So, I want to have two radio button in separate place, I have been trying to search for the solution and everyone suggests to use radiolist which is not possible in my case.
If I put it like this (work_part_time button) : (below)
<div class="row">
<div class="col-sm-2">
<?= $form->field($model, 'work_part_time')->radio(['label' => 'yes', 'value' => 1])?>
</div>-
<div class="col-sm-3">
<?= $form->field($model, 'hour_week')->textInput(['type' => 'number', 'placeholder' => 'Hour/Week'])->label(false)?>
</div>
<div class="col-sm-3">
<?= $form->field($model, 'part_time_rate')->textInput(['type' => 'number', 'placeholder' => 'rate/hour(SGD)'])->label(false)?>
</div>
</div>
<div class="form-group">
<?= $form->field($model, 'work_part_time')->radio( [0 => 'No'])->label('No')?>
</div>
<hr>
<div class="row">
<div class="col-sm-2">
<?= $form->field($model, 'work_part_time')->radio(['label' => 'yes', 'value' => 1])?>
</div>-
<div class="col-sm-3">
<?= $form->field($model, 'hour_week')->textInput(['type' => 'number', 'placeholder' => 'Hour/Week'])->label(false)?>
</div>
<div class="col-sm-3">
<?= $form->field($model, 'part_time_rate')->textInput(['type' => 'number', 'placeholder' => 'rate/hour(SGD)'])->label(false)?>
</div>
</div>
<div class="form-group">
<?= $form->field($model, 'work_part_time')->radio( [0 => 'No'])->label('No')?>
</div>
<hr>
I only can get 0 for the value.
Anyone has found the solution for this?
Yii will assign a checked or unchecked value to the radio button depending on the value of the stored attribute, so if the value is 0 it will check the button that has the value 0. Your problem seems to have been the hidden input that Yii automatically generates. As others have suggested, you need to set this to null if you want more than one radio button for the same field.
If the user checks another button, then all other radio buttons with the same name will become unchecked. The name of the attribute is generated automatically by Yii when it creates the button.
Try these for your radio buttons:
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 1', 'value' => 1, 'uncheck' => null]) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 2', 'value' => 0, 'uncheck' => null]) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 3', 'value' => 2, 'uncheck' => null]) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option4', 'value' => 3, 'uncheck' => null]) ?>
Each button needs a different value, and this is the value that will be stored in your field when the record is saved.
There can only ever be one button checked, so if you have multiple buttons with the same value, and the same name, as you seem to have in your examples, then only the last one in the set will be checked. I don't know of a way round this. I suggest you use <formgroup> to split up your form into logical sections, each section relating to whether work_part_time is yes or no. You seem to have started doing this!
I've personally checked your code in my system. This code returning 0 or 1 (in respective of selection of radio button.) It's working Fine. You can use my code.
In below code, if you want to give label as Work part time or some other, put in ->label('Work Part Time');
.
. // Your code
<?= $form->field($model, 'work_part_time')->radioList([1 => 'yes', 0 => 'No'])->label('Work Part Time'); ?>
.
. // Your code
AND
1) If you want to check 'Yes' as default checked radio button, then you have to assign like this <?php $model->status_id = 1?>
.
. // Your code
<?php $model->status_id = 1?>
<?= $form->field($model, 'work_part_time')->radioList([1 => 'yes', 0 => 'No'])->label('Work Part Time'); ?>
.
. // Your code
2) If you want to check 'No' as default checked radio button, then you have to assign like this <?php $model->status_id = 0?>
.
. // Your code
<?php $model->status_id = 0?>
<?= $form->field($model, 'work_part_time')->radioList([1 => 'yes', 0 => 'No'])->label('Work Part Time'); ?>
.
.//Your code
try this:
<?php echo $form->radioButton($model,'user_work_part_time',array('value'=>1,'uncheckValue'=>null));
$form->radioButton($model,'user_work_part_time',array('value'=>0,'uncheckValue'=>null));
?>
add 'uncheckValue'=>null in htmlOption array it will work.
If you substitute "uncheck" with "uncheckValue", the voted answer will work, except it will only post the value of the last radio on the list if selected and 0 for the rest. To make it work and post selected values as intended, I added some JS code to it. Hope it will help someone down the road.
<!-- Complete solution for Radio buttons on separate places Yii2 together with its JS function-->
<php use yii\web\View; ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 1', 'value' => 1, 'uncheckValue' => null,'onChange'=>' if($(this).prop("checked")){ var radioValue = 1;$(this).val(radioValue); var radioName = "Model[work_part_time]"; RadioSelected(radioName,radioValue);}else{$(this).val("")};']) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 2', 'value' => 0, 'uncheckValue' => null,'onChange'=>' if($(this).prop("checked")){ var radioValue = 0; $(this).val(radioValue); var radioName = "Model[work_part_time]"; RadioSelected(radioName,radioValue);}else{$(this).val("")};']) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 3', 'value' => 2, 'uncheckValue' => null,'onChange'=>' if($(this).prop("checked")){ var radioValue = 2; $(this).val(radioValue); var radioName = "Model[work_part_time]"; RadioSelected(radioName,radioValue);}else{$(this).val("")};']) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option4', 'value' => 3, 'uncheckValue' => null,'onChange'=>' if($(this).prop("checked")){var radioValue = 3; $(this).val(radioValue); var radioName = "Model[work_part_time]"; RadioSelected(radioName,radioValue);}else{$(this).val("")};']) ?>
<?php
//JS Function
$RadioONSeparatePlaceJS = <<<JS
function RadioSelected(radioName,radioValue)
{
$('input[name="' + radioName + '"]').val(radioValue);
}
JS;
$this->registerJs($RadioONSeparatePlaceJS,View::POS_HEAD);
?>
Thank you for the great answer Joe Miller.
I want to pass a php variable to modal window , what i am doing is opening a modal window using this link , but i want to pass a variable to this link and get same variable in modal window , i try to to do this to append a text in some div but it return html that i am unable to get in query
echo CHtml::link(
'Set Recipe', '', array(
'class' => 'testclass',
'id' => $finalDate,
'data-toggle' => 'modal',
'data-target' => '#myModal',
'fahadVar' => $finalDate
));
and when i click this button i got modal window how to get variable set in button
Here is simple modal code of yiibooster
<div class="modal-body">
<p>One fine body...</p>
</div>
<div class="modal-footer">
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'type' => 'primary',
'label' => 'Save changes',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'Close',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
thanks in advance
You should create a Widget.
Note: I copied below from another post. It works like charm.
First Create a new widget. Let say the name is CategoryWidget. Put this widget under components directory protected/components.
class CategoryWidget extends CWidget {
public function run() {
$models = Category::model()->findAll();
$this->render('category', array(
'models'=>$models
));
}
}
Then create a view for this widget. The file name is category.php. Put it under protected/components/views
category.php
<?php if($models != null): ?>
<ul>
<?php foreach($models as $model): ?>
<li><?php echo $model->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Then call this widget from your main layout.
main.php
// your code ...
<?php $this->widget('CategoryWidget') ?>
I have a CGridView wigdet with CCheckBoxColumn like this:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
),
'title',
....
),
));
Question: how to submit to controller action the checked values? I understand that I need a form, submit button, but I need a clear explanation where to put things, so that search boxes on the top appear.
Thanks in advance.
You do not absolutely need another form. You can just use a link with additional javascript attached to it.
To get the checked values, you can call the javascript function $.fn.yiiGridView.getChecked(containerID,columnID), see here, it returns an array containing the ids.
Full example (with ajax):
In your view:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'example-grid-view-id', // the containerID for getChecked
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
'id'=>'example-check-boxes' // the columnID for getChecked
),
'title',
....
),
));
?>
<div id="for-link">
<?php
echo CHtml::ajaxLink('SomeLink',Yii::app->createUrl('somecontroller/someaction'),
array(
'type'=>'POST',
'data'=>'js:{theIds : $.fn.yiiGridView.getChecked("example-grid-view-id","example-check-boxes").toString()}'
// pay special attention to how the data is passed here
)
);
?>
<div>
In your controller:
...
public function actionSomeaction(){
if(isset($_POST['theIds'])){
$arra=explode(',', $_POST['theIds']);
// now do something with the ids in $arra
...
}
...
}
...
You could also use json string, instead of simple string, in the data we pass by ajax (from the view), but then instead of explode(), you would use json_decode() (in the controller). Also it would be better to validate/sanitize the ids before use.
Check out the documentation for CHtml::ajaxLink to know more about ajax links.
Note that the example is a little crude, since i haven't put in checks for empty array of checked ids.
This one works with CSRF protection and updates the GridView.
<?php
echo CHtml::ajaxLink('ClickMe',Yii::app()->createUrl('controller/action'),
array(
'type'=>'POST',
'data'=>'js:{"ids" : $.fn.yiiGridView.getChecked("grid_view_id","check_box_id").toString(),"YII_CSRF_TOKEN" : "'.Yii::app()->request->csrfToken.'"}',
'success'=>"$('#grid_view_id').yiiGridView.update('grid_view_id')"
));
?>
If you wrap your Gridview into a simple form you can send the checkboxs selected to the value,
An example:
View
<form id="frmSubmit">
<?php
echo CHtml::dropDownList('user_id'
, null
, CHtml::listData(User::model()->findAll(), 'USER_ID', 'FULLNAME')
);
echo CHtml::ajaxSubmitButton('Save'
, Yii::app()->createUrl('query/ajaxUpdate')
, array('success' => 'js:reloadGrid', )
, array('class' => 'btn btn-success'));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'query-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'id' => 'query',
'class'=>'CCheckBoxColumn',
'selectableRows' => '2',
),
'Contact',
'Email',
'Company',
),
)); ?>
</form>
<script type="text/javascript">
function reloadGrid(data) {
$.fn.yiiGridView.update('query-grid');
}
</script>
Controller
public function actionAjaxUpdate()
{
var_dump($_POST);
}