Yii Ajax - Update A Textfield & Dropdown Based On A Dropdownlist Value

Hi guys,

I have two dropdown list and one text box (Country state mobile-Country-code)

while i’m selecting the country from dropdown list it displays related states in the country,

i want to display both states and mobile country code

The states in the other table , but the mobile-country-code is in the same country table…

please help me… :(

my controller:


/**

	 * To load the states for the country

	 */

	public function actionGetStates() {

		$country_id = $_REQUEST['region_id'];

		foreach(Subregions::items($country_id) as $value => $name)

	    {

	        echo CHtml::tag('option',

	                   array('value'=>$value),CHtml::encode($name),true);

	    }

	}

form.php:




<td>Country *</td>

		<td>

		<?php echo $form->dropDownList($userModel, 'region_id',  Regions::items(), 

			array( 'ajax' => array( 'type' => 'post',

                       'url' => $this->createUrl('users/getstates'),

'beforeSend' => 'function() { $("#Users_subregions_id").empty(); 

 $("#Users_subregions_id").append("<option>Loading... </option>"); 

this.url = this.url + "&region_id=" + $("#Users_region_id").val();}',

                        'update' => '#Users_subregions_id'),

			'style' => 'width: 140px;'),

			array('type'=>'POST', 'url'=>Users::createUrl('GetCountryCode'),

                 'success'=>'js:function(data{$("#country_code").val(data)}',

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

                 array('onchange'=>"$('#country_code').val($(mcountrycode).text()"));

        ?>

    	</td>

        <td>State *</td>

        <td><?php $states = Subregions::items($userModel->region_id);

	        if (count($states) == 0) {

	         	$states = array( '0' => 'Select country first');

	        }

			echo $form->dropDownList($userModel, 'subregions_id', $states, array('style' => 'width: 140px;'));

        ?>

        </td>

<td>Contact Number</td>

        <td ><?php echo $form->textField($userModel, "mcountrycode" 

        // array('id'=>'country_code',

        // 'name'=>'country_code')

	)?></td> 



please someone could give an idea how to update an textfield & dropdownlist based on a dropDownList value using ajax?

In your function actionGetStates(), you only returned state options, I did not see where you can get mcountrycode.

If as what you said, mcountrycode can get from country table, then you can assign mcountrycode via country selected.

So far I cannot understand your code about "region_id".

Not clear if you want to get mcountrycode by call another function "GetCountryCode" in the code.

I’m wondering how you fit the country codes in the same table as as your countries. Do you have multiple rows for the same country in that table? Or, is the MCC a varchar field with each MCC separated by some delimiter? Or, are you just saving the most common MCC for each country? I really can’t see how this can be done all in a single table without having problems or being very inefficient in some situations.

While I can’t answer your question without more info I can tell you that using a textbox for mcountrycode input is probably not going to be the best option. There are a finite number of those (max = 999) so I’d put that in a dropdown too. This will help reduce input errors.