Add anchor class in codeigniter - codeigniter

How can i add anchor class in codeigniter
This is normal html tag:
<a class="tooltips" href="index.html"><span>Log out</span><i class="lnr lnr-power-switch"></i></a>
But when i add class="tooltips" it comes out of a tag:
<?php echo anchor('User_Controller/settings','class="tooltips"<span>Logout</span><i class="lnr lnr-power-switch"></i>');?>
This is what i get:
class="tooltips"<span>Logout</span><i class="lnr lnr-power-switch"></i>

You have syntax error, you can try this ,
<?php echo anchor('User_Controller/settings', '<span>Logout</span><i class="lnr lnr-power-switch"></i>', 'class="tooltips"'); ?>
for refference : Ancher helper

$atts = array(
'width' => 800,
'height' => 600,
'scrollbars' => 'yes',
'status' => 'yes',
'resizable' => 'yes',
'screenx' => 0,
'screeny' => 0,
'window_name' => '_blank'
'class' => 'btn btn-primary'
);
echo anchor('news/local/123', 'Click Me!', $atts); //it's example

Related

CakePHP 3 : Retrieving data from database using Ajax

I am working on a CakePHP 3 project which is having a Apply Coupon form.
I want to apply the coupon using Ajax.
The view of coupon form is
<?= $this->Form->create(null, [
'url' => ['controller' => 'Coupons', 'action' => 'checkCoupon'],
'name' => 'checkCoupon'
]) ?>
<?= $this->Form->input('coupon_code', [
'type' => 'text',
'placeholder' => 'Apply Coupon Code',
'label' => false
]) ?>
<?= $this->Form->submit('Apply Coupon') ?>
and the checkCoupon action in CouponsController is
public function checkCoupon()
{
$this->request->onlyAllow('ajax'); // No direct access via browser URL
if ($this->request->is('post')) {
$couponCode = $this->request->data['coupon_code'];
$couponCheck = $this->Coupons->find('all', [
'conditions' => [
'coupon_code' => $couponCode
]
]);
if ($couponCheck->count() === 1) {
$coupon = $couponCheck->first();
$valid_till = $coupon->valid_till;
$dt = new Time($valid_till);
$date = $dt->format('Y-m-d');
if ($date >= date('Y-m-d')) {
echo 'Coupon is Valid. Discount of '.$coupon->value.'has been applied';
} else {
echo 'Coupon is Expired';
}
} else {
echo 'This is not a valid coupon code';
}
}
}
I want the $coupon->value and $coupon->id to be retrieved and added to the checkout link as
<?= $this->Html->link(__('Confirm Checkout'), ['controller' => 'ServiceRequests', 'action' => 'confirmCheckout', $service->id, $primaryAddressId, $serviceArea->id, $coupon->id], ['class' => 'btn btn-block btn-success']) ?>
The Apply Coupon form is in checkout action of RequestsController
Also the form is working well. I have checked it by removing the onlyAllow('ajax') line and printing values in check_coupon.ctp view.
How could I do it using Ajax ?
Edit 2 : checkout.ctp
<div class="form-info coupon">
<?= $this->Form->create(null, [
'url' => ['controller' => 'Coupons', 'action' => 'ajax_checkCoupon'],
'name' => 'checkCoupon',
'id' => 'checkCoupon'
]) ?>
<?= $this->Form->input('coupon_code', [
'type' => 'text',
'placeholder' => 'Apply Coupon Code',
'label' => false
]) ?>
<label class="hvr-sweep-to-right">
<?= $this->Form->submit('Apply Coupon', ['id' => 'applyCoupon']) ?>
</label>
<label id="couponUpdate"></label>
<label id="loading" style="display:none;">Loading...</label>
<?php
$data = $this->Html->script('#checkCoupon')->serializeForm(['isForm' => true, 'inline' => true]);
$this->Html->script('#checkCoupon')->event(
'submit',
$this->Html->script(
[
'controller' => 'Coupons',
'action' => 'ajax_checkCoupon'
],
[
'update' => '#couponUpdate',
'data' => $data,
'async' => true,
'dataExpression' => true,
'before' => "$('#loading').fadeIn();$('#applyCoupon').attr('disabled','disabled');",
'complete' => "$('#loading').fadeOut();$('#applyCoupon').removeAttr('disabled');"
]
)
);
?>
</div>
Error : Call to a member function serializeForm() on string on line 18 in checkout.ctp

dependent dropdown when edit in yii

Problem in dependent dropdowns when editing in my yii application.
While editing, the drop downs are not automatically selected.
In my view,
array('class' => 'CButtonColumn',
'header' => 'Manage',
'template' => '{update} {view} {delete}',
'htmlOptions' => array('width' => '20%'),
'buttons' => array(
'update' => array(
'label' => '',
'imageUrl' => '',
'options' => array('class' => 'glyphicon glyphicon-pencil'),
),
'view' => array(
'label' => '',
'imageUrl' => '',
'options' => array('class' => 'glyphicon glyphicon-eye-open'),
),
'delete' => array(
'label' => '',
'imageUrl' => '',
'options' => array('class' => 'glyphicon glyphicon-remove'),
),
),
),
<div class="form-group">
<label for="reg_input" class="req">Course</label>
<?php
$course = CHtml::listData(Course::model()->findAll(), 'courseid', 'course_name');
echo CHtml::activeDropDownList($model, 'courseid', $course, array(
'empty' => 'Select Course', 'class' => "form-control",
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('Assignment/Fetchbatch'),
'update' => '#' . CHtml::activeId($model, 'batchid'))));
?>
<?php echo $form->error($model, 'courseid', array('class' => 'school_val_error')); ?>
</div>
<div class="form-group">
<label for="reg_input" class="req">Batch</label>
<?php
$batch = CHtml::listData(Batch::model()->findAll(), 'batchid', 'batch_name');
echo $form->dropDownList($model, 'batchid', $batch, array('prompt' => 'Select Batch',
'class' => "form-control",
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('Assignment/Fetchsubject'),
'update' => '#' . CHtml::activeId($model, 'subjectid'))));
echo $form->error($model, 'batchid', array('class' => 'school_val_error'));
?>
</div>
Second dropdown get the data, to change of first dropdown. At this condition the dropdown will not be selected automatically. Because when editing, that value is not there. So I fixed this problem, my code is above this.

Yii Refresh Grid On DropDown change

In YII views folder i have test module and admin.php file to manage contents are below and i render form here where i put the code of form and dropdown in it , i want that grid refresh value of status change in dropdown
Suppose i select "Approved" than Grid show the data where status is approved
<?php
Yii::app()->clientScript->registerScript('dropdown', "
$('.dropdown-form form').submit(function(){
$('#testimonial-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage Testimonials</h1>
<div class="dropdown-form">
<?php $this->renderPartial('_dropdownform',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'testimonial-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'created_by',
'test_name',
'test_email',
'comments',
'created_at',
/*
'status',
'approved_on',
'approved_by',
*/
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Form below is _dropdownform , it contain a form and dropdown from this dropdown i am choosing the value of status
<div class="wide form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'action' => Yii::app()->createUrl($this->route),
'method' => 'get',
));
?>
<div class="row">
<?php
echo CHtml::dropDownList('status', '', array(0 => 'New', 1 => 'Approved', 2 => 'Declined'), array(
'prompt' => 'Select Status',
'ajax' => array(
'type' => 'POST',
'url' => Yii::app()->createUrl('testimonial/loadthedata'),
//or $this->createUrl('loadcities') if '$this' extends CController
'update' => '#testimonial-grid', //or 'success' => 'function(data){...handle the data in the way you want...}',
'data' => array('status' => 'js:this.value'),
)));
?>
</div>
<div class="row buttons">
<?php //echo CHtml::submitButton('Search'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- search-form -->
AND THE CODE IN MY CONTROLLER OR URL GIVEN IN DROPDOWN TO FETCH DATA IS FOLLOWING ACTION BUT I DONT KNOW HOW TO FETCH DATA FROM THIS FUNCTION AND PASS TO GRID VIEW
public function actionloadthedata() {
if (isset($_POST['status'])) {
$status = $_POST['status'];
if($status==0){
$status='New';
}
if($status==1){
$status='Approved';
}
if($status==2){
$status='Declined';
}
Testimonial::model()->findByAttributes(array('status'=>$status));
}
}
You can use CGridView property filterCssClass to link the grid filter, for example
$this->widget('CGridView', array(
'id' => 'my-list',
'filterCssClass' => '#filterFormId .filter',
And there is filter form
<?php $form = $this->beginWidget('CActiveForm', array(
'id' => 'filter-fomr-id',
)); ?>
<div class="filter clearfix">
<?php echo $form->dropDownList($model, 'name', [0=>'all', '1'=>'some else']); ?>
</div>
Replace #filterFormId .filter on jquery selector specific to you form. In other words, set id attribute for filter form, then use "#THISID .row".
In your gridview file, make sure you have this code:
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#ad-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
then in the CGridView definition:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'testimonial-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
...
array(
'name'=>'Status',
'filter'=>CHtml::dropDownList('YourModel[status]', $model->status, array(0 => 'New', 1 => 'Approved', 2 => 'Declined'), array('empty' => '--all--') ),
'value'=>'( $data->status == 0) ? "New": ( $data->status == 1) ? "Approved" : "Declined"',
'htmlOptions' => array(
'style' => 'width: 40px; text-align: center;',
),
),
...
array(
'class'=>'CButtonColumn',
),
),
));
// CGridView
In order to save if/else in the 'value' section, you can implement a method in your model that returns the string associated to the integer.
It works great, just with the default Yii admin.php view, which you can edit as much as you need.
Update
Added support for empty status, does not filter query results
Thanks #Alex for your help but i am successful to make filterdropdown for grid , code is following but will you please tell me that i want that grid show only values where status=New , how i can do when page load grid show values where status is New
but first i paste the working code of dropdown filter for grid
Here is my admin.php file
<?php
$this->breadcrumbs = array(
'Testimonials' => array('index'),
'Manage',
);
$this->menu = array(
array('label' => 'List Testimonial', 'url' => array('index')),
array('label' => 'Create Testimonial', 'url' => array('create')),
);
?>
<h1>Manage Testimonials</h1>
<!-----------drop down form------------->
<?php
Yii::app()->clientScript->registerScript('dropdownfilter', "
$('.dropdown-form form #staticid').change(function(){
$.fn.yiiGridView.update('testimonial-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<div class="dropdown-form">
<?php
$this->renderPartial('_dropdownfilter', array(
'model' => $model,
));
?>
</div><!-- search-form -->
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'testimonial-grid',
'dataProvider' => $model->search(),
// 'filter' => $model,
'columns' => array(
'id',
'created_by',
'test_name',
'test_email',
'comments',
'created_at',
'status',
array(
'class' => 'CButtonColumn',
),
),
));
?>
Here is my render partial form where i place the static drop dow
<div class="wide form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'action' => Yii::app()->createUrl($this->route),
'method' => 'get',
));
?>
<div class="row">
<?php
echo CHtml::dropDownList('staticid', '', array('0' => 'New', '1' => 'Approved', '2' => 'Declined'), array(
// 'onChange' => 'this.form.submit()',
'ajax' => array(
'type' => 'POST', //request type
)));
?>
</div>
<?php $this->endWidget(); ?>
And Here is code of my adminaction in controller
public function actionAdmin() {
$model = new Testimonial('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['staticid'])) {
$getStatus = $_GET['staticid'];
if ($getStatus == 0)
$status = 'New';
if ($getStatus == 1)
$status = 'Approved';
if ($getStatus == 2)
$status = 'Declined';
$model->status = $status;
}
if (isset($_GET['Testimonial']))
$model->attributes = $_GET['Testimonial'];
$this->render('admin', array(
'model' => $model,
));
}
Now i want that when i actionadmin triggered first time it show status=New values

cakephp pass select value ajax

I have two select buttons and want to filter records, when user select any option I have to fire a event and pass the values of buttons to the controller's action. my code is as under
view
<?php echo $this->Form->create('Employee'); ?>
<fieldset>
<div class="pure-control-group">
<?php echo $this->Form->input('designation', array("label" => "Designation",'type' => 'select', 'id'=>'DesignationType','options' => $settings,'empty' => 'select'));?>
<?php echo $this->Form->input('district', array("label" => "District",'type' => 'select', 'id'=>'districtType','options' => $district,'empty' => 'select'));?>
</div>
</fieldset>
<?php echo $this->Form->end();
$this->Js->get('#DesignationType')->event('change',
$this->Js->request(array(
'controller'=>'employees',
'action'=>'getByCategory'
), array(
'update'=>'#success',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
?>
<div id="success"></div>
controller
public function getByCategory(){
$design_id =$this->request->data['Employee']['designation'];
$district_id =$this->request->data['Employee']['district'];
$this->layout = 'ajax';
}
when i click designation then i m able to get the value of designation but not able to get the value of district .And following error occur
Undefined index: district [APP\Controller\EmployeesController.php, line 18]
so, how can i get the value of district;
Try to send the form id in data variable. that is
Try this :
$data = $this->Js->get('#YourFormId')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#DesignationType')->event('change',
$this->Js->request(array(
'controller'=>'employees',
'action'=>'getByCategory'
), array(
'update'=>'#success',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $data
))
);

CJuiDatePicker validation messages isn't working

Hi.
I have a problem with form-validation in Yii framework.
Here is my VIEW code:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'search-form',
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'focus' => array($model, 'ccc'),
'clientOptions' => array(
'validateOnSubmit' => true,
),
));
?>
<?php
echo $form->errorSummary($model);
?>
<div class="row">
<?php echo $form->labelEx($model, 'input'); ?>
<?php echo $form->textField($model, 'input', array('class' => 'input-medium', 'maxlength' => 11,)); ?>
<?php echo $form->error($model, 'input'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'attribute' => 'date',
'name' => 'date',
'model' => $model,
'language' => 'ru',
'options' => array(
'dateFormat' => 'dd/mm/y',
'showAnim' => 'slideDown',
'changeMonth' => true,
'changeYear' => true,
'showOn' => 'button',
'constrainInput' => 'true',
),
'htmlOptions' => array(
'style' => 'height:15px; width:6em'
),
));
?>
<?php echo $form->error($model, 'date'); ?>
</div>
<?php $this->endWidget(); ?>
Nothing special. But validation messages working only for textField (Ajax requests are sending only with onChange textField).
How to enable CJuiDatePicker validation messages?
You just have to give the right id to your CJuidatepicker object, use CHtml::getIdByName to create the id value, try to the name of the html element there, it must be something like
'id' => CHtml::getIdByName(get_class($model) . '[' . $attribute . ']')
it would become something like this:
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id' => CHtml::getIdByName(get_class($model) . '[date]'),
'attribute' => 'date',
'name' => 'date',
'model' => $model,
'language' => 'ru',
'options' => array(
'dateFormat' => 'dd/mm/y',
'showAnim' => 'slideDown',
'changeMonth' => true,
'changeYear' => true,
'showOn' => 'button',
'constrainInput' => 'true',
),
'htmlOptions' => array(
'style' => 'height:15px; width:6em'
),
));
wonde's answer "you should include a value" didn't work for me...
This is what worked:
Yii CActiveForm date validation
views/site/login.php originally had:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
Things worked when this was changed to:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
),
See:
http://sky-walker.net/temp/test/yii/testdate/index.php?r=site/login
This is what i did and it works, i think you should include a value
<?php echo $form->labelEx($model,'reportDate'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array( 'model'=>$model,
'attribute'=>'reportDate',
**'value'=>$model->reportDate,**
'options'=>array(
'showButtonPanel'=>true,
'changeYear'=>true,
'changeMonth'=>true,
'autoSize'=>true,
'dateFormat'=>'yy-mm-dd',
'defaultDate'=>$model->reportDate,
),
));
?>
<?php echo $form->error($model,'reportDate'); ?>
this problem can be shoved simply by adding $form->error($model,'end_date') after CJuiDatePicker.
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id' => CHtml::getIdByName(get_class($model) . '[date]'),
'attribute' => 'date',
'name' => 'date',
'model' => $model,
'language' => 'ru',
'options' => array(
'dateFormat' => 'dd/mm/y',
'showAnim' => 'slideDown',
'changeMonth' => true,
'changeYear' => true,
'showOn' => 'button',
'constrainInput' => 'true',
),
'htmlOptions' => array(
'style' => 'height:15px; width:6em'
),
));
echo $form->error($model,'dateTo');// added to enaple clint validation

Resources