Yii Ajax Update depending dropdown selection - php

I have dropdown list where there are five or six items (here items are services). I used here ajax request which updates the id “package_id” from the URL 'url'=>$this->createUrl('servicePackage'), once I select any service item. Till now, I have done successfully. But I want that ajax should use different URL and update different id i.e. “registrationid” if I select 4th number item, otherwise it should update “package_id”.
Is it possible?
Is it possible?
The form is:
<div class="form-group">
<?php echo $form->labelEx($model,'service_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php echo $form->dropDownList($model,'service_id',CHtml::listData(Service::model()->findAll(),'id', 'name'),
array(
'class' => 'form-control',
'prompt'=>'Select a Service',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('servicePackage'),
'update'=>'#'.CHtml::activeId($model, 'package_id'), // ajax updates package_id, but I want ajax update registration_id if I select item no 4
'data'=>array('service_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'service_id'); ?>
</div>
<div class="form-group" id="packageid">
<?php echo $form->labelEx($model,'package_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
echo $form->dropDownList($model,'package_id',$model->getServicePackage($model->service_id),array(
'class' => 'form-control','prompt'=>'Select a Package',
'ajax' =>
array('type'=>'POST',
'url'=>$this->createUrl('packagePrice'), //url to call.
'update'=>'#price', //selector to update
'data'=>array('package_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'package_id'); ?>
</div>
<div class="form-group" id="registrationid">
<?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$registrants = Registration::model()->findAll();
$data = CHtml::listData($registrants,'id', function($registrants) {
return CHtml::encode($registrants->name.' ('.$registrants->id.')');
});
echo $form->dropDownList($model,'registration_id',$data,
array(
'class' => 'required form-control chzn-select',
'prompt'=>'Select a Registrant',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('Order'), //url to call.
'update'=>'#'.CHtml::activeId($model, 'order_id'), //selector to update
'data'=>array('registration_id'=>'js:this.value'),
)
));
?>
</div>
<?php echo $form->error($model,'registration_id'); ?>
</div>

Related

Decode JSON like text from CakePHP to Codeigniter [duplicate]

This question already has answers here:
How to use php serialize() and unserialize()
(10 answers)
Closed 3 years ago.
I'm tasked to change the framework from CakePHP to Codeigniter. In the database there is this JSON like text in one column in order to define the values of 4 select inputs. The text looks like -
a:4:{i:58;s:1:"1";i:59;s:1:"0";i:60;s:1:"0";i:61;s:1:"0";}
Basically it says a(array size which is 4), i(being the id) and in s(i dont know about the first integer but all of its values is just 1 the second integer with the "" means it is the 0=>'None', 1=>'Yes', 2=>'NaN').
This is the previous programmer's code in CakePHP
<?php
$options_services = array(0=>'None', 1=>'Yes', 2=>'NaN');
?>
<div class="box-body">
<div class="form-group ">
<div class="row">
<div class="col-md-6">
<label> Select Service Category</label>
<div>
<?php echo $this->Form->input('catservice_id', array('options'=>$catservices,'div' => false, 'label' => false, 'class' => 'form-control','type'=>'select','empty'=>'Please Select Category')); ?>
</div>
</div>
<div class="col-md-6">
<label> Title <span class="required">*</span></label>
<div>
<?php echo $this->Form->input('title', array('div' => false, 'label' => false, 'class' => 'form-control')); ?>
</div>
</div>
</div>
</div>
<div class="form-group ">
<div class="row">
<?php if(!empty($service_list)): ?>
<?php foreach($service_list as $key=>$values): ?>
<div class="col-md-3">
<label><?php echo $values;?> </label>
<div>
<?php echo $this->Form->input('cleaning.'.$key, array('options'=>$options_services,'div' => false, 'label' => false, 'class' => 'form-control','type'=>'select')); ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<?php
echo $this->Html->link('Back', array('controller' => 'services', 'action' => 'index'), array('escape' => false, 'class' => 'btn btn-info'));
?>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
i certainly means id
i want to use the JSON like text to be able to use this
I tried json_decode in Codeigniter but it doesnt work. You guys have any idea how I can decode this? Would help me a lot. Thank you!
I got it thanks to #GetSet help.
Using unserialize
See this question : How to use php serialize() and unserialize()

How to put Glyphicons Icons in input?

How can I add a Glyphicons Icons in the input below?
A part of the code:
class GrupoUsuarioForm{
$this->setAttributes(array(
'method' => 'POST',
'class' => 'formGrupoUsuario',
'name' => 'formGrupoUsuario'
));
$this->setInputFilter(new GrupoUsuarioInputFilter());
$dataCadastro = new Text('data_grp');
$dataCadastro->setValue(date('d/m/Y'))
->setAttributes(array(
//'style' => 'width: 20%',
'id', 'dataGrp',
'readOnly' => 'true'
));
$dataCadastro->setLabel('Data do Cadastro');
$this->add($dataCadastro);
Would like this return HTML
<label>Textbox with calendar icon</label>
<div class="input-append"><input type="text" id="" name="">
<span class="add-on"><i class="icon-calendar"></i></span></div>
Where excatly do you want put this icon?
You can render form's elements as you want.
In controller:
public function myAction()
{
return new ViewModel([
'form' => new GrupoUsuarioForm()
]);
}
In your view:
// HTML code here
...
<?php $form = $this->form; ?>
<?php $form->prepare(); ?>
<?php echo $this->form()->openTag($form); ?>
...
<?php echo $this->formLabel($form->get('data_grp')); ?>
<div class="input-group">
<div class="input-append">
<?php echo $this->formInput($form->get('data_grp')); ?>
<span class="add-on"><i class="icon-calendar"></i></span>
</div>
</div>
...
<?php echo $this->form()->closeTag(); ?>

Enter the user currently logged on to form

I have this code
enter <div class="formularz">
<div class="row">
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<div class="col-md-6">
<?php echo $form->field($model, 'name')->label('Imię i nazwisko')?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'email')->label('Adres email') ?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'phone')->label('Telefon') ?>
</div> here
How can i get in this inputs values of actualy logged user for example i refresh page and i want to have filled inputs with some data.
I take you're using Yii. Here is the documentation of what you're asking:
http://www.yiiframework.com/doc-2.0/yii-bootstrap-activeform.html#field%28%29-detail
Just add any attribute you want inside an array and plug it in as the third parameter of the field method.
<?php echo $form->field($model, 'email', ['inputOptions' => ['value' => 'my Value']])->label('Adres email') ?>

Yii update from modal window

I need to update a modal window and show it when I click in the edit button.
CONTROLLER:
public function actionUpdateAjax($id)
{
$contratos = ZfContratos::model()->findByPk($id);
$this->renderPartial('//ZfContratos/_form_update', array('model'=>$contratos), false, true);
}
INDEX:
<?php $this->beginWidget(
'bootstrap.widgets.TbModal',
array('id' => 'actualizar_contrato')
); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Actualizar contrato</h4>
</div>
<div class="modal-body">
<?php $this->renderPartial('//ZfContratos/_form_update', array('model'=>$contrato));?>
</div>
<div class="modal-footer">
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'CANCELAR',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
AND VIEW _contratos
At this moment I have this:
<?php echo CHtml::link('EDITAR', array('//ZfInmuebles/UpdateAjax', 'id'=>$data->zf_contrato_id), array('class'=>'btn', 'id'=>'vermas')); ?>
But I need that to be an ajaxbutton or ajaxlink, that refresh the div "actualizar_contrato" and show it.
This is just a reference implementation to give you an idea of how to do this, do modify as per your requirements
// your view
// Button to trigger modal
<?php echo CHtml::ajaxLink('EDITAR',array('//ZfInmuebles/UpdateAjax', 'id'=>$data-> zf_contrato_id), array(
'type'=>'POST',
"success"=>'js:function(html){
$("#actualizar_contrato >.modal-body").html(html);
$("#actualizar_contrato").modal("show");
}
'));?
.....
// modal
<?php $this->beginWidget(
'bootstrap.widgets.TbModal',
array('id' => 'actualizar_contrato')
); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Actualizar contrato</h4>
</div>
<div class="modal-body">
<?php
$contrato = new ZfContratos;
$this->renderPartial('//ZfContratos/_form_update', array('model'=>$contrato));
?>
</div>
<div class="modal-footer">
<?php echo CHtml::ajaxLink('Save', // Link Text
array('//ZfContratos/update', 'id'=>$data->zf_contrato_id), //url
array(// ajaxOptions
'data'=> 'js:$("#id_of_the_form").serialize()' ,
'type'=>'POST',
'success'=>'js:function(html){$("#actualizar_contrato >.modal-body").html(html) }'
)
)
);?>
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'CANCELAR',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
.....
And now in your controller modify as like below
//ZfContratos Controller
public function actionUpdate(){
// Your regular logic for saving
if(Yii::app()->request->isAjaxRequest){
if($model->save()){
$this->renderPartial("_mvsavedView",array('model'=>$model)) //OR echo "Saved Successfully";
} else {
print_r($model->getErrors());
}
Yii::app()->end();
}
}

how to insert multi records in yii session using ajaxSubmitButton

I have been developing an application in Yii framework. At this point, I fall in an issue that is, I have an Order form where I select a registrant (all registrants come from database) from a dropdown, an item (all product items come from database) from dropdown and a input textbox where I type the quantity. There is a "ajaxSubmitButton" button to send all these values to the controller "actionCart" using Ajax. After receiving all values in the controller, I want to put all values in session variable. When I add another new item, the session values are being replaced what I want to hold all newly added items into the session variable. In this circumstance, what should I do. Please help me. I am giving my code snippets below:
in form:
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'order-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal' , 'enctype'=>'multipart/form-data', ),
)); ?>
<div class="alert alert-info" xmlns="http://www.w3.org/1999/html">
<p class="note">Fields with <strong><span class="required">*</span></strong> are required.</p>
</div>
<?php echo $form->errorSummary($model); ?>
<div class="form-group">
<?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$data = CHtml::listData(Registration::model()->findAll(),'id', 'name');
echo $form->dropDownList($model,'registration_id',$data,array('class' => 'form-control chzn-select','prompt'=>'Select a Registrant'));
?>
</div>
<?php echo $form->error($model,'registration_id'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'item', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$data = CHtml::listData(Products::model()->findAll(),'id', 'name');
echo $form->dropDownList($model,'item',$data, array('class'=>'form-control chzn-select' , 'id'=>'item', 'prompt'=>'Select an Item')); ?>
<?php
?>
</div>
<?php echo $form->error($model,'item'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'quantity', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-2">
<?php
echo $form->textField($model,'quantity',array('class' => 'form-control','size'=>60,'maxlength'=>11));
?>
</div>
<?php echo $form->error($model,'quantity'); ?>
</div>
<div class="form-group">
<div class="col-lg-8 pull-right">
<?php
echo CHtml::ajaxSubmitButton('Add to Cart',Yii::app()->createUrl('admin/order/cart'),
array(
'type'=>'POST',
'update'=>'#cartResult',
),
array('class'=>'btn btn-primary btn-sm',));
?>
</div>
</div>
<div class="form-group">
<div id="cartResult" class="col-lg-12">
</div>
</div>
<?php $this->endWidget(); ?>
</div>
In controller:
public function actionCart()
{
if(isset($_POST["Order"])){
$item = $_POST["Order"];
$registration_id = $item["registration_id"];
$productId = $item["item"];
$quantity = $item["quantity"];
$quantity = $item["quantity"]=='' ? 1 : $item["quantity"];
$productInfo = Products::model()->findByPk(array('id'=>$productId));
$totalPrice = $productInfo->price * $quantity;
$session = Yii::app()->session;
$session['cart'] = array("product_id" => "$productId" , "product_name" => "$productInfo->name", "quantity" => "$quantity","price" => "$productInfo->price");
}
}

Categories