Dropdown get Value

i try this tutorial http://www.yiiframe




<?

echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),

array(

'ajax' => array(

'type'=>'POST', //request type

'url'=>CController::createUrl('site/test'), //url to call.

//Style: CController::createUrl('currentController/methodToCall')

'update'=>'#city_id', //selector to update

//'data'=>'js:javascript statement' 

//leave out the data key to pass all form values through

))); 

 

//empty since it will be filled by the other dropdown

echo CHtml::dropDownList('city_id','', array());

?>



and controller :




public function actionTest(){

		$tag = $_POST['country_id'];


		echo CHtml::tag('option',array('value'=>'A' ),CHtml::encode('A - ' .  $tag),true);

	

	}



i want display data ‘A - 1’ when clicked USA and ‘A - 2’ when clicked France’

1 -> USA Value and 2 is France Value from country_id dropdown.

but the data does not appear as I expected.

what is wrong with my code.

thank you

check your HTML source to see if the ID of the second dropdown is city_id, I guess it’s not

thank for response.

this HTML source :




<select name="country_id" id="country_id">

<option value="1">USA</option>

<option value="2">France</option>

<option value="3">Japan</option>

</select>

<select name="city_id" id="city_id">

</select>	</div><!-- content -->

</div>



everything seems fine… I would suggest to use firebug to see what values are sent with the ajax call… and what is returned…

I’ve tried to debug using firebug, and there’s no data sent.

is thare any solution for this ?

1741

bug post.JPG

well, I found the solution.

I add this tag:




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

    'id'=>'sample-form',

    'enableAjaxValidation'=>true

)); ?>


.....................


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




Can anybody assist

This is my code under _search.php

<?php

/* @var $this BiodataPelajarController */

/* @var $model BiodataPelajar */

/* @var $form CActiveForm */

?>

<div class="wide form">

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

'action'=&gt;Yii::app()-&gt;createUrl(&#036;this-&gt;route),


'method'=&gt;'post',


    'enableAjaxValidation'=&gt;true

)); ?>

&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;label(&#036;model,'id'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'id'); ?&gt;


&lt;/div&gt;

<!-- <div class="row">

	&lt;?php echo &#036;form-&gt;label(&#036;model,'id_program_pengajian'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'id_program_pengajian'); ?&gt;


    &lt;/div&gt;--&#62;

<!-- <div class="row">

	&lt;?php echo &#036;form-&gt;label(&#036;model,'Pusat Pengajian'); ?&gt;


            &lt;?php echo &#036;form-&gt; dropDownList(&#036;model, 'id_program_pengajian',


                               CHtml::listData(PusatPengajian::model()


                               -&gt;findAll(array('order'=&gt;'nama_pusat_pengajian')),'id','nama_pusat_pengajian'));


            ?&gt;


   &lt;/div&gt;

–>

<!-- <div class="row">

	&lt;?php echo &#036;form-&gt;label(&#036;model,'Pusat Pengajian'); 


            &#036;list = CHtml::listData(PusatPengajian::model()


                               -&gt;findAll(array('order'=&gt;'nama_pusat_pengajian')),'id','nama_pusat_pengajian');


            ?&gt;


             &lt;?php echo &#036;form-&gt; dropDownList(&#036;model, 'id_program_pengajian',


                               &#036;list, array ('empty'=&gt;'Pilihan pusat')); ?&gt;


          


   	&lt;/div&gt;--&#62;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;label(&#036;model,'Pusat Pengajian'); ?&gt;


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'id_program_pengajian','',


                    array(0=&gt;'Pilihan Pusat Pengajian', 1=&gt;'Bahasa Dan Linguistik',2=&gt;'Bahasa, Kesusteraan Dan Kebudayaan Melayu',


                          3=&gt;'Media Dan Komunikasi',4=&gt;'Psikologi Dan Pembangunan Manusia', 5=&gt;'Sejarah, Politik Dan Strategi',


                          6=&gt;'Sosial, Pembangunan Dan Persekitaran'),


                    array('ajax'=&gt;'POST',


                          'url'=&gt;CController::createUrl('LoadProgramPengajian'),


                          'update'=&gt;'#id_programpengajian',


                          )


                          ); 


                  echo CHtml::dropDownList('id_programpengajian','', array()); ?&gt;     


   	&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;label(&#036;model,'matrik'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'matrik',array('size'=&gt;7,'maxlength'=&gt;7)); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;label(&#036;model,'nama'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'nama',array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;label(&#036;model,'ingatan30_60'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'ingatan30_60'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton('Search'); ?&gt;


&lt;/div&gt;

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

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

  1. And this is my controller file :

.

.

.

 public function actionLoadProgramPengajian()


   {


    &#036;id_program_pengajian=(int)&#036;_POST['id_program_pengajian'];


    var_dump(&#036;id_program_pengajian);


    if (&#036;id_program_pengajian=0)


            &#036;data=array(0=&gt;'States:');


    else if (&#036;id_program_pengajian==1)


            &#036;data=array(1=&gt;'Alaska', 2=&gt;'California');      


    else if (&#036;id_program_pengajian==2)


            &#036;data=array(1=&gt;'Orleans', 2=&gt;'Bordeaux');


    else if (&#036;id_program_pengajian==3)


            &#036;data=array(1=&gt;'Hokkaido', 2=&gt;'Okinawa');


    foreach(&#036;data as &#036;value=&gt;&#036;name)


            echo CHtml::tag('option', array('value'=&gt;&#036;value), CHtml::encode(&#036;name), true);


   }        

But i keep on getting PHP warning : Invalid argument supplied for foreach()

That is because of nothing send by ajax as data:




<?php echo $form->dropDownList($model,'id_program_pengajian','', array(0=>'Pilihan Pusat Pengajian',  1=>'Bahasa Dan Linguistik',2=>'Bahasa, Kesusteraan Dan Kebudayaan  Melayu', 3=>'Media Dan  Komunikasi',4=>'Psikologi Dan Pembangunan Manusia', 5=>'Sejarah,  Politik Dan Strategi', 6=>'Sosial, Pembangunan Dan Persekitaran'),

 	array('ajax'=>'POST',

         	'url'=>CController::createUrl('LoadProgramPengajian'),

         	'type'=>'POST',

         	'data'=>array('id_program_pengajian'=>'js:this.value'),

         	'update'=>'#id_programpengajian',

                              )

                              ); 

                      echo CHtml::dropDownList('id_programpengajian','', array()); ?>     

       	</div>



ok…tq for the respond…I’ve edit my script as below but i’m getting the same warning "Invalid argument supplied for foreach() "…Tq inadvance for any assistance …i’m still new with yii

&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;label(&#036;model,'Pusat Pengajian'); ?&gt;


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'id_program_pengajian','',


                    array(0=&gt;'Pilihan Pusat Pengajian', 1=&gt;'Bahasa Dan Linguistik',2=&gt;'Bahasa, Kesusteraan Dan Kebudayaan Melayu',


                          3=&gt;'Media Dan Komunikasi',4=&gt;'Psikologi Dan Pembangunan Manusia', 5=&gt;'Sejarah, Politik Dan Strategi',


                          6=&gt;'Sosial, Pembangunan Dan Persekitaran'),


                    array('ajax'=&gt;array(


                        'type'=&gt;'POST',


                          'url'=&gt;CController::createUrl('LoadProgramPengajian'),


                          'update'=&gt;'#id_programpengajian',


                          )


                          )


                          ); 


                  echo CHtml::dropDownList('id_programpengajian','', array()); ?&gt;     


   	&lt;/div&gt;

sorry…latest upadte as below but still not working :

&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;label(&#036;model,'Pusat Pengajian'); ?&gt;


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'id_program_pengajian','',


                    array(0=&gt;'Pilihan Pusat Pengajian', 1=&gt;'Bahasa Dan Linguistik',2=&gt;'Bahasa, Kesusteraan Dan Kebudayaan Melayu',


                          3=&gt;'Media Dan Komunikasi',4=&gt;'Psikologi Dan Pembangunan Manusia', 5=&gt;'Sejarah, Politik Dan Strategi',


                          6=&gt;'Sosial, Pembangunan Dan Persekitaran'),


                    array('ajax'=&gt;array(


                        'type'=&gt;'POST',


                          'url'=&gt;CController::createUrl('LoadProgramPengajian'),


                          'data'=&gt; array('id_program_pengajian' =&gt; 'js:this.value'),


                          'update'=&gt;'#id_programpengajian',


                          )


                          )


                          ); 


                  echo CHtml::dropDownList('id_programpengajian','', array()); ?&gt;     


   	&lt;/div&gt;