Yii Framework Forum: ajax disable textfield from selected dropdownlist - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

ajax disable textfield from selected dropdownlist Rate Topic: -----

#1 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 21 August 2012 - 08:59 PM

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
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 22 August 2012 - 03:35 AM

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

#3 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 22 August 2012 - 08:16 AM

View Postbennouna, on 22 August 2012 - 03:35 AM, said:

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 :
  • enable textfield1 and disable textfield2
  • disable textfield1 and enable textfield2
  • enable both textfield
  • disable both textfield


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

#4 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 22 August 2012 - 08:43 AM

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'];

0

#5 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 22 August 2012 - 06:27 PM

View Postbennouna, on 22 August 2012 - 08:43 AM, said:

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
0

#6 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 23 August 2012 - 12:08 AM

So

Quote

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

What do you get?
0

#7 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 23 August 2012 - 02:59 AM

View Postbennouna, on 23 August 2012 - 12:08 AM, said:

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.
0

#8 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 23 August 2012 - 04:33 AM

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
0

#9 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 23 August 2012 - 07:28 AM

View Postbennouna, on 23 August 2012 - 04:33 AM, said:

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){}
0

#10 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 23 August 2012 - 08:25 AM

Quote

still the same result..
var_dump($_POST)

didn't change anything


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?
0

#11 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 23 August 2012 - 09:45 AM

View Postbennouna, on 23 August 2012 - 08:25 AM, said:

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
0

#12 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 23 August 2012 - 10:19 AM

Quote

1) in actionBts()

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?
…

Quote

2) sure, here it is..

Please change the line above in actionBts() into
$cek = (int) $_POST['Transaksi']['id_pembayaran'];

Quote

3)Do you mean monitoring ajax using firebug?

Yes

Quote

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

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

Quote

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

Ok, can you post a screenshot or Firebug?
0

#13 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 23 August 2012 - 11:14 AM

View Postbennouna, on 23 August 2012 - 10:19 AM, said:

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?
…


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

Quote

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

Nothing..

Quote

Ok, can you post a screenshot or Firebug?

Attached File(s)


0

#14 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 23 August 2012 - 12:56 PM

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?
0

#15 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 23 August 2012 - 05:51 PM

View Postbennouna, on 23 August 2012 - 12:56 PM, said:

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
0

#16 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 23 August 2012 - 06:03 PM

create.php renderPartial _form.php
and _form.php renderPartial _form_clause.php
0

#17 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 23 August 2012 - 06:25 PM

Ok, so please provide create.php view as well
0

#18 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 23 August 2012 - 06:32 PM

View Postbennouna, on 23 August 2012 - 06:25 PM, said:

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)); ?>

0

#19 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 23 August 2012 - 06:42 PM

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


Because: http://www.yiiframew...rPartial-detail

Edit:

And of course, in your actionBts
$cek = (int) $_POST['Transaksi']['id_pembayaran'];

0

#20 User is offline   qikio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 10-August 12
  • Location:Jakarta

Posted 23 August 2012 - 07:36 PM

View Postbennouna, on 23 August 2012 - 06:42 PM, said:

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


Because: http://www.yiiframew...rPartial-detail

Edit:

And of course, in your actionBts
$cek = (int) $_POST['Transaksi']['id_pembayaran'];



no effect ???
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users