ajax disable textfield from selected dropdownlist

Good morning all,

I want to disable/enable 2 textfields from selected dropdownlist using ajax.

this is my code

form.php


	<div class="row">

		<?php echo $form->labelEx($model,'id_pembayaran'); ?>

                <?php 

                    echo $form->dropDownList($model,'id_pembayaran',CHtml::listData(Pembayaran::model()->findAll(), 'id_pembayaran', 'jenis_pembayaran'),array('empty'=>'--Please Select One--'),

                    array( 

                    'ajax'=>array(

                    'type'=>'POST',

                    'url'=>CController::createUrl('Transaksi/bts'),

                    'update'=>'#clause',

                    ))); 

                ?>

		<?php echo $form->error($model,'id_pembayaran'); ?>

	</div>


        <div id="clause">

		<?php $this->renderPartial('_form_clause', array('flag'=>$flag,'model'=>$model)); ?>

	</div>

TransaksiController.php


            public function actionBts()

            {

                $model=new Transaksi();

                $cek=$_POST['id_pembayaran'];

                //$cek=(int)Yii::app()->request->getParam('id_pembayaran');

                if($cek==1||$cek==3||$cek==9||$cek==12||$cek==20||$cek==22||$cek==23||$cek==24||$cek==26){

                    $flag="3";

                }

                else if($cek==4||$cek==6||$cek==7||$cek==10||$cek==13||$cek==14||$cek==15||$cek==18||$cek==21||$cek==25){

                    $flag="2";

                }

                else if($cek==2||$cek==5||$cek==8||$cek==17){

                    $flag="1";

                }

                else{

                    $flag="0";

                }

                $this->renderPartial('_form_clause',array('flag'=>$flag,'model'=>$model), false, true);

            }

_form_clause.php


<?php


/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

$form = $this->beginWidget('CActiveForm',array('id'=>'transaksi-form'));

if($flag=="0"){

                echo $form->labelEx($model,'debet');

		echo $form->textField($model, 'debet', array('readonly'=>'true')).'<br/>';

                echo $form->labelEx($model,'kredit');

		echo $form->textField($model, 'kredit', array('readonly'=>'true'));

}else if ($flag=="1"){

                echo $form->labelEx($model,'debet');

		echo $form->textField($model, 'debet').'<br/>';

		echo $form->labelEx($model,'kredit');

		echo $form->textField($model, 'kredit', array('readonly'=>'true'));

}else if ($flag=="2"){

                echo $form->labelEx($model,'debet');

		echo $form->textField($model, 'debet', array('readonly'=>'true')).'<br/>';

		echo $form->labelEx($model,'kredit');

		echo $form->textField($model, 'kredit');               

}else if ($flag=="3"){

                echo $form->labelEx($model,'debet');

		echo $form->textField($model, 'debet').'<br/>';

		echo $form->labelEx($model,'kredit');

		echo $form->textField($model, 'kredit');               

}

$this->endWidget();

?>

I’m so confused, why the results are the same (disable 2 textfield) no matter dropdownlist which I choose?

*sorry for my english

Hello. Please explain what you are trying to do exactly. I can’t personally see it.

Thx for reply ::)

I have 1 dropdownlist and 2 textfields,

Dropdownlist taken from database, it returns id_pembayaran.

from selected dropdownlist, there are four possibility result :

[list=1]

[*]enable textfield1 and disable textfield2

[*]disable textfield1 and enable textfield2

[*]enable both textfield

[*]disable both textfield

[/list]

But I confused why the result is only disable both textfield?

Have you tried to echo both your $cek and $flag to be sure what their values are?

Maybe everything’s working correctly after all, and that you should (int) parse your $cek in your Bts method?


$cek = (int) $_POST['id_pembayaran'];

I have tried that, but it doesn’t works

So

What do you get?

can you tell me where I put the echo?

I just add:

in actionCreate.php


$cek=0;

...

$this->render('create',array(

'model'=>$model,

'model2'=>$model2,

'flag'=>$segment1,

'cek'=>$cek,

));



then I change:


$this->renderPartial('_form_clause',array('flag'=>$flag,'model'=>$model), false, true);

to be like this:


$this->renderPartial('_form_clause',array('flag'=>$flag,'model'=>$model, 'cek'=>$cek), false, true);

then I add ‘value’=>$cek in _form_clause.php like this:


echo $form->textField($model, 'debet', array('readonly'=>'true', 'value'=>$cek)).'<br/>';

the result display 0.

Well from your view’s dropdown, it should be rather


$cek = (int) $_POST['Transaksi']['id_pembayaran'];

(if your model’s class is ‘Transaksi’)

If it doesn’t work, please add


var_dump($_POST)

in actionBts

still the same result…


var_dump($_POST)

didn’t change anything

so, I try to put it in the _form.php

then, it display array(0){}

  1. Where did you put it please?

  2. Paste again your full action and view.

  3. Can you monitor you ajax request at all?

  4. Do you have POSTed elements?

  5. What’s the ajax request response? Is it a success (200 OK)? Or else?

  1. in actionBts()

  2. sure, here it is…

actionCreate()


	public function actionCreate()

	{

                $segment1 = "0";

                $cek=0;

		$model=new Transaksi;

                $model2=new Pajak;

                

                //nomor kuitansi otomatis

                $no=(int)$model->no_kuitansi;

                $transaksimax=  Transaksi::model()->findBySql('SELECT max(no_kuitansi) AS no_kuitansi FROM transaksi');

                //$model->no_kuitansi=$transaksimax->no_kuitansi+1;

                $nomer=$transaksimax->no_kuitansi+1;

                if(strlen($nomer)==1){

                    $model->no_kuitansi = '00'.$nomer;

                }

                else if(strlen($nomer)==2){

                    $model->no_kuitansi = '0'.$nomer;

                }

                else{

                    $model->no_kuitansi=$nomer;

                }

                

                //tanggal input transaksi

                $model->tanggal_transaksi=  date('Y-m-d H:i:s');


		if(isset($_POST['Transaksi'],$_POST['Pajak']))

		{

			$model->attributes=$_POST['Transaksi'];

                        $model2->attributes=$_POST['Pajak'];

                        

                        $valid=$model->validate();

		        if($model2->id_pajak!=null)

                            $valid=$model2->validate() && $valid;

                        else

                            $valid=$model2->validate(array('nilai_ppn','nilai_pph21','nilai_pph22','nilai_pph23')) && $valid;


                        

			if($valid)

			{

                                if($model->save(false))  

				{  

					$model2->id_transaksi = $model->id_transaksi;  

					$model2->save(false);

					$this->redirect(array('view','id'=>$model->id_transaksi));

				}

                        }

		}


		$this->render('create',array(

			'model'=>$model,

                        'model2'=>$model2,

                        'flag'=>$segment1,

                        'cek'=>$cek,

		));

	}

actionBts()


            public function actionBts()

            {

                $model=new Transaksi();

                $cek=(int)$_POST['id_pembayaran'];

                var_dump($_POST);

                //$cek=(int)Yii::app()->request->getParam('id_pembayaran');

                if($cek==1||$cek==3||$cek==9||$cek==12||$cek==20||$cek==22||$cek==23||$cek==24||$cek==26){

                    $flag="3";

                }

                else if($cek==4||$cek==6||$cek==7||$cek==10||$cek==13||$cek==14||$cek==15||$cek==18||$cek==21||$cek==25){

                    $flag="2";

                }

                else if($cek==2||$cek==5||$cek==8||$cek==17){

                    $flag="1";

                }

                else{

                    $flag="0";

                }

                $this->renderPartial('_form_clause',array('flag'=>$flag,'model'=>$model, 'cek'=>$cek), false, true);

            }

_form.php


<div class="wide form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'transaksi-form',

	'enableAjaxValidation'=>false,

)); ?>




	<p class="note">Kotak isian dengan tanda <span class="required">*</span> harus diisi.</p>


	<?php echo $form->errorSummary($model); ?>

<div class="row">

		<?php echo $form->labelEx($model,'id_pembayaran'); ?>

                <?php /*echo $form->dropDownList($model,'id_pembayaran',CHtml::listData(Pembayaran::model()->findAll(), 'id_pembayaran', 'jenis_pembayaran'),array('onchange'=>'{tes1();}','empty'=>'--Please Select One--'));*/?>

                <?php 

                    echo $form->dropDownList($model,'id_pembayaran',CHtml::listData(Pembayaran::model()->findAll(), 'id_pembayaran', 'jenis_pembayaran'),array('empty'=>'--Please Select One--'),

                    //echo CHtml::dropDownList('id_pembayaran','',array('default'=>'---','1'=>'D','2'=>'K','3'=>'DK'),

                    array( 

                    'ajax'=>array(

                    'type'=>'POST',

                    'url'=>CController::createUrl('Transaksi/bts'),

                    'update'=>'#clause',

                    ))); 

                ?>

		<?php echo $form->error($model,'id_pembayaran'); ?>

	</div>


        <div id="clause">

		<?php $this->renderPartial('_form_clause', array('flag'=>$flag,'model'=>$model, 'cek'=>$cek)); ?>

	</div>

	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>

        


<?php $this->endWidget(); ?>


</div><!-- form -->

3)Do you mean monitoring ajax using firebug?

4)Sorry, I don’t understand. what element?

5)Nothing. No respons in firebug when I select the dropdownlist

But why in the pasted code, I still see this?


            public function actionBts()

            {

                $model=new Transaksi();

                $cek=(int)$_POST['id_pembayaran']; // you forgot the model name?

…

Please change the line above in actionBts() into


$cek = (int) $_POST['Transaksi']['id_pembayaran'];

Yes

I was asking if, when monitoring, you see the elements that are in the POST array in the ajax request

Ok, can you post a screenshot or Firebug?

Sorry, actually I have try that… but, It’s no change, then I undo it… hehehe

Nothing…

Ok, thanks for your answers and the ss.

So your bts action is never actually called.

Another thing, your flow begins with actionCreate right. And you posted the code in the comments #7 and #11. In it, I can see that you render the create view. But in the post #1, you are showing the form view, and in the #11 the _form view.

Are you sure we’re talking about the correct view?

Sorry for my typo… the correct thing, in #1 is _form.php , not form.php

create.php renderPartial _form.php

and _form.php renderPartial _form_clause.php

Ok, so please provide create.php view as well


<?php

$this->breadcrumbs=array(

	'Transaksi'=>array('index'),

	'Entri',

);


$this->menu=array(

	array('label'=>'Daftar Transaksi', 'url'=>array('index')),

	array('label'=>'Kelola Transaksi', 'url'=>array('admin')),

);

?>


<h1>Entri Transaksi</h1>


<?php echo $this->renderPartial('_form', array('model'=>$model,'model2'=>$model2, 'flag'=>$flag, 'cek'=>$cek)); ?>

change the last line into


<?php echo $this->renderPartial('_form', array('model'=>$model,'model2'=>$model2, 'flag'=>$flag, 'cek'=>$cek), false, true); ?>

Because: http://www.yiiframework.com/doc/api/1.1/CController#renderPartial-detail

Edit:

And of course, in your actionBts


$cek = (int) $_POST['Transaksi']['id_pembayaran'];

no effect ???