Cannot update textfield based on dropdown list Yii - php

I have 3 dependent dropdowns, and a textfield being dependent to the last dropdown. If one of the value in the last dropdown, I want the textfield value dynamically changes based on the selected value (it's from the same Database table).
This is the view of the third dropdown:
<div class="row" id="id_subkeg">
<?php echo $form->labelEx($model, 'id_subkeg'); ?>
<?php
echo $form->dropDownList($model, 'id_subkeg', array(), array(
'style' => 'width: 100%',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('dynamicSatuan'),
'update' => '#' . CHtml::activeId($model, 'satuan'),
//'update'=>'#seksi',
'data' => array('id_subkeg' => 'js:this.value'),
)
)
);
?>
<?php echo $form->error($model, 'id_subkeg'); ?>
</div>
This is the textfield view:
<div class="row" id="satuan">
<?php echo $form->labelEx($model, 'satuan'); ?>
<p class="hint" style="font-size: 80%">Contoh: Dokumen.</p>
<?php echo $form->textField($model, 'satuan', array('style' => 'width: 98%;', 'readonly' => FALSE)); ?>
<?php echo $form->error($model, 'satuan'); ?>
</div>
And this is the action in the controller:
public function actionDynamicSatuan() {
//$data = Subkegiatan::model()->findByPk($_POST['id_subkeg']);
$data = Subkegiatan::model()->findByPk('id_subkeg=:id_subkeg', array(':id_subkeg' => (int) $_POST['id_subkeg']));
echo $data->satuan;
}
But it's not been working for days. I don't know which part I've missed. My guess is that the dropdown is a dependent dropdown to the one above it. So I must have missed at some part.
Any help is highly appreciated.
UPDATE:
After days of searching, finally got it:
public function actionDynamicSatuan() {
$param_country = (int) $_POST['id_subkeg'];
$maxOrderNumber = Yii::app()->db->createCommand()
->select('satuan')
->from('subkegiatan')
->where('id_subkeg = ' . $param_country)
->queryScalar();
echo '<b>SATUAN: '. $maxOrderNumber.'</b>';
//echo CHtml::tag('input', array('type' => 'text', 'value' => $maxOrderNumber));
}

Instead of update, use success callback:
echo $form->dropDownList($model, 'id_subkeg', array(), array(
'style' => 'width: 100%',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('dynamicSatuan'),
'data' => array('id_subkeg' => 'js:this.value'),
'success'=> 'function(data) {
$("#your_id_here").empty();
$("#your_id_here").append(data);
}'
)
)
);

Related

session based multistep form cakephp

Basically i have 3 forms.
I have one action in my form and i want create 3 steps form using session.
I want when i click the next button it validates the form1,if successful then redirects to form2..
like wise for form3 but keep all the data in session and in third step the action goes runs and add all the data to database.
I only need to check conditions from session before my insert goes run in my action addjob.
Plz help me to do this.
My main question is how to get all the post data in one session variable and check step by step before insert..
i am learner in cakephp and i have not enough idea about session.
if you are a expert then plz do this.
public function addjob($id = NULL) {
$this->layout = "layout_registration_old";
$this->loadcategory();
$this->loadcargo();
//$this->loadsubcategory();
$this->getCountries();
$this->getstates();
if ($this->request->is('put') || $this->request->is('post')) {
if (isset($id)) {
$this->Job->id = $id;
} else {
$this->request->data['Job']['status'] = 1;
$this->request->data['Job']['job_type'] = 1; //this is used to update the job type private or public.
$this->Job->create();
}
$this->Job->set($this->request->data);
if ($this->Job->AddEdit()) { // ADDEdit is the validation name in model
if ($this->Job->save($this->request->data['Job'], false)) {
if (isset($id)) {
$this->Session->setFlash(__('Job has been updated sucessfully.'));
} else {
$this->Session->setFlash(__('Job has been added succesfully.'));
}
$this->redirect(array('controller' => 'jobs', 'action' => 'index'));
}
} else {
$errors = $this->Job->validationErrors;
$this->Session->setFlash(__('Please check your entry.'), 'flash_error');
}
}
if (isset($id)) {
$this->request->data = $this->Job->find('first', array('conditions' => array('id' => base64_decode($id))));
}
}
form - 1
<?php echo $this->Form->create('Job', array('url' => array('controller' => 'jobs', 'action' => 'addjob')));?>
<?php echo $this->Form->input('customer_name',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('customer_no',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('customer_email',array('div' => false, 'label' => false));?>
<?php
echo $this->Form->input('transport_type', array(
'type' => 'select',
'label' => false,
'class' => 'select',
'options' => array(
1 => 'Road'
),
));
?>
<?php echo $this->Form->submit('Save', array('class' => "navigation_button btn btn-primary btn-sm", 'div' => false)); ?>
<?php echo $this->Form->end(); ?>
form - 2
<?php echo $this->Form->create('Job', array('url' => array('controller' => 'jobs', 'action' => 'addjob')));?>
<?php echo $this->Form->input('fname',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('lname',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('email',array('div' => false, 'label' => false));?>
<?php echo $this->Form->submit('Save', array('class' => "navigation_button btn btn-primary btn-sm", 'div' => false)); ?>
<?php echo $this->Form->end(); ?>
form - 3
<?php echo $this->Form->create('Job', array('url' => array('controller' => 'jobs', 'action' => 'addjob')));?>
<?php echo $this->Form->input('shop',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('business',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('status',array('div' => false, 'label' => false));?>
<?php echo $this->Form->submit('Save', array('class' => "navigation_button btn btn-primary btn-sm", 'div' => false)); ?>
<?php echo $this->Form->end(); ?>
Instead of saving the data from first 2 forms in a session, you should implement a multi part form using any javascript solution (Form wizard). Validate the data of each step using javascript or via ajax (if needs to be done dynamically) before moving to the next step and submit form at the end so that you don't have to play with sessions.
Example link
PS - This could go as a comment to the question but I haven't gained enough reputations to post comments yet :(

Drupal 7 customize user registration form

I am new to drupal.
I need to customize a registration form by adding fields like id,mobile etc.
Can I accomplish this by creating a custom module?
If yes could anyone please help me with a brief idea on creating and overriding the default user registration submit function. I have to insert these details to another table and also have to pass the data as a service request.
Ive created a custom module with function
module_form_alter(&$form,&$form_state,$form_id){
$form['#submit'] = 'module_form_submit';
if($form_id == 'user_register_form'){
//print_r($form_id);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('id'),
'#default_value' => '',
'#size' => 60,
'#maxlength' => 15,
'#required' => TRUE,
);
}
}
function module_form_submit($form, &$form_state){
echo "test";
exit();
}
module_form_alter is being called and I can see the new field on the registration screen but the submit function is still not called. I need to override the default drupal register submit.
I already have the following function in my theme template.php
function templatename_theme() {
$items = array();
$items['user_login'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'portal') . '/templates',
'template' => 'user-login',
'preprocess functions' => array(
'portal_preprocess_user_login'
),
);
$items['user_register_form'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'portal') . '/templates',
'template' => 'user-register-form',
'preprocess functions' => array(
'portal_preprocess_user_register_form'
),
);
$items['user_pass'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'portal') . '/templates',
'template' => 'user-pass',
'preprocess functions' => array(
'portal_preprocess_user_pass'
),
);
return $items;
}
user-register-form.tpl.php
<div class="form-group">
<?php print drupal_render_children($form); ?>
</div>
And page--user--register.tpl.php with the html
<div id="login-page">
<div class="container">
<div class="form-login" >
<h2 class="form-login-heading"><img src="<?php echo drupal_get_path('theme', 'portal') . '/images/logo.png'; ?>" width="100"><?php echo $createaccount; ?></h2>
<div class="login-wrap">
<?php
$elements = drupal_get_form("user_register_form");
$form = drupal_render($elements);
echo $form ?>
<?php if ($messages):?>
<div id="messages-console" class="clearfix">
<div class="grid_12">
<div class="mt-grid-fix">
<?php print $messages; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
Is this approach fine or should I change this approach inorder to make my custom module functional?
You can go to:
admin/config/people/accounts/fields
and add the fields that you need.
In the fieldset "User settings" don't forget check "Display on user registration form."

Cakephp form has no data on submit

Using cakephp 1.3 I fill out a form, and upon debugging the submit, I notice that
data:$("#submit-478065271").closest("form").serialize()
is empty. Why would something like that happen? I've checked that the form actually has data, which I can serialize manually. Here is the submit code:
echo $this->Js->submit(
'Sign up',
array(
'class' => 'btn btn_submit fr register_submit register_btn_align',
'url' => array('controller' => 'email_guides', 'action' => 'subscribe'),
'before' => '$(".error-message").remove();' . $this->Js->get('#loading')->effect(
'fadeIn',
array('buffer' => false)
),
'complete' => $this->Js->get('#loading')->effect(
'fadeOut',
array('buffer' => false)
) . 'debugger;',
'success' => 'if(data.success) {
$("#CustomUserFirstName").val("");
$("#CustomUserEmail").val("");
$("#EmailGuidesUserStartDate").val("' . date('d/m/Y', strtotime('+1 Weekday')) . '");
$("#EmailGuidesUserTerms").attr("checked", false);
$("#signupModal").hide();
} else {
$("#signupModal").hide();
}
'type' => 'json'
)
);
?>
<?php echo $this->Form->end(); ?>
UPDATE: I've noticed that the form is not a parent of the submit element ... which is very bizarre. This would explain why .closest("form") returns an empty array.

Yii. Dependent Dropdown

I try make dependent select.
My Model
Region
id
region
City
id
city
region_id
Customer
id
region
city
address
phone
In my views(Customer form):
<div class="row">
<?php
echo $form->dropDownList($model,'region',CHtml::listData(Region::model()->findAll(), 'id', 'region'),
array(
'prompt'=>'Select Region',
'ajax' => array(
'type' => 'POST', //My method type
'url' => CController::createUrl('myController/LoadRegions'), //This is my request/ajax URL
array('id'=>'js:this.value'), //I'm passing the selected dropdonw value.
'dataType' => 'JSON',
'success'=>'js:function(data)' //The functionaliy after success
. '{'
. ' var html="";'
. ' $.each(data,function(i,obj)'
. ' {'
. ' html+="<option value=obj.City_id>"+obj.City_city+"</option>"'
. ' });'
. ' $("#User_City_id").html(html);' //ID of regions dropdown list
. '}'
)));
echo CHtml::dropDownList($model,'City_id', array(), array('prompt'=>'Select City'));
?>
And in my controller(Customer):
public function actionLoadRegions()
{
$Region_id=$_POST['region'];
$criteria=new CDbCriteria();
$criteria->select=array('Region_id, Region_region');
$criteria->condition='Region_id='.$Region_id;
$criteria->order='Region_region';
$RegionAry= Region::model()->findAll($criteria);
$ary=array();
foreach($RegionAry as $i=>$obj)
{
$ary[$i]['Region_id']=$obj->Region_id;
$ary[$i]['Region_region']=$obj->Region_region;
}
echo json_encode($ary);
}
But code is not work. Error Object of class Customer could not be converted to string. Why?
This will work
Your Form
'success' => 'js:function(data) {
$("#User_City_id").html(data);
}'
Your Controller function
public function actionLoadRegions()
{
$Region_id=$_POST['region'];
$criteria=new CDbCriteria();
$criteria->select=array('Region_id, Region_region');
$criteria->condition='Region_id='.$Region_id;
$criteria->order='Region_region';
$RegionAry= Region::model()->findAll($criteria);
$ary=array();
foreach($RegionAry as $i=>$obj)
{
$ary[$i]['Region_id']=$obj->Region_id;
$ary[$i]['Region_region']=$obj->Region_region;
}
echo CHtml::dropDownList('dropdown_name', $selected, $ary);
}
I am not familier with Yii but I think you are treating a integer variable as a string so that its showing error .
You sure about this-
echo CHtml::dropDownList($model,'City_id', array(), array('prompt'=>'Select City'));
try instead
echo CHtml::dropDownList($model,City_id, array(), array('prompt'=>'Select City'));
use something like this:
echo $form->dropDownList(
$model,
'state_id',
$array,
array(
'class' => 'form-control',
'ajax' => array(
'type' => 'POST',
'url' => $this->createUrl('getregions'),
'update' => '#Customers_region_id',
'data'=>array('state_id'=>'js:this.value')
)
)
); ?>
but you have to change name of field state_id, update and url on your defining of dropDownList.
maybe it will help.

Yii Dropdown List Empty Value as Default

I have a dropdownlist in my _form model and I want to add a empty value (which I want as default). I have the following:
In _form:
<?php echo $form->labelEx($model,'country_id'); ?>
<?php echo $form->dropDownList($model,'country_id',Country::items(),array('empty' => '--Select a country--')); ?>
<?php echo $form->error($model,'country_id'); ?>
In Model Country:
public static function items()
{
return CHtml::listData(Country::model()->findAllBySql(
'SELECT * from country'),
'id', 'name');
}
Even my empty option is in the first row in dropdownlist, the 1st country in list shows as default.
I tried:
<?php echo $form->dropDownList($model,'country_id',
Country::items(),array('empty'=>'--Select a country--',
'options'=>
array(
'3'=>array('selected'=>'selected')
)
));
?>
In this way I can choose the default option, but cant set it to empty value, just countries that came from model:items.
Any idea?
Are you sure that country_id property of your model is not set to anything when you print the dropdown list? The following works for me if $model instance is created using new Country() operator but not by populating properties from database:
<?php echo $form->dropDownList(
$model,
'country_id',
Country::items(),
array(
'empty'=>'--Select a country--')
);
?>
Read documentation. There is 'prompt' parameter.
Try this:
<?php
echo $form->dropDownList($model,'country_id',Country::items(), array(
'prompt' => '--Select a country--'
));
?>
See more details here http://www.yiiframework.com/forum/index.php/topic/11195-how-to-edit-the-default-option-in-dropdownlist/
You always can do something like array_merge in your items method
public static function items()
{
return array_merge(array(''=>'--Select a country--'), CHtml::listData(Country::model()->findAllBySql(
'SELECT * from country'),
'id', 'name'));
}
I believe youre looking for:
echo $form->dropDownList($model,'country_id',Country::items(),array('prompt'=>''));
if you use yiibooster maybe this will help
<?php echo $form->dropDownListGroup(
$model,
'kode_cuti_sub2',
array(
'empty'=>'--Select a country--',
'widgetOptions' => array(
'data' => array('Something ...', 'Pilih Jenis Cuti'=>Chtml::listData(Cuti::model()->cuti_sub2(),'kode','jenis_cuti')),
'options' => array(
'placeholder' => 'Pilih NIP Pegawai',
'width' => '100%',
),
),
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
)
); ?>
in my case it's worked

Categories