Display result when select from checkbox in the same page with ajax

First thing is , i hope it will help other thats all,

Suppose i have two table named: work_order and clients

work_order contains a client id which is related table of clients

Now i want a form(in work_order model) with a dropdown list containing work orders from work_order model

when i select one from the work_order dropdownlist, the associative client name (for that work_order) will be displayed in the desired location.

So, here i done this:

In my views/work_order/_form.php:





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

 <?php

                    	echo $form->dropDownList($model, 'work_order_no', CHtml::listData(WorkOrder::model()->findAll(), 'id', 'id'), array(

                        	'prompt' => 'Select One',

                        	'ajax' => array(

                        	'type' => 'POST',

                        	'dataType'=>'json',

                        	'url' => CController::createUrl('WorkOrder/clientFromThis'),

                      	

                        	'success' => 'function(data) {

                            	$("#Work_order_client_view").html(data.client);

                           	

                         	}',

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

                    	),

                    	));

                    	?>


 <div class="total_qty_lebel_custom">Client of this work order</div>

                	<div id="Work_order_client_view">

                    	

                 	</div>




and in your WorkOrder controller make a function :




 public function actionClientFromThis() {

    	$data = WorkOrder::model()->findAll('id=:work_order_no', array(':work_order_no' => $_POST['work_order_no']));

    	foreach ($data as $d):

     	$client_name=  Clients::model()->findByPk($d->client_id);

    	$client=$client_name->client_name;

        	echo $array = CJSON::encode(array(

            	'client' => $client,

        	));

    	endforeach;

    	if (!$_POST['work_order_no']) {

        	echo $array = CJSON::encode(array(

            	'client' => '',

        	));

    	}

	}



Thats it, if anything i missed please leave reply, many many thanks in advance.

This is similar to creating a dependent dropdown list.

Though I hope it will help someone, but I have to move the topic to the Tips forum ;)

Sure :rolleyes:

I use this too but it not work for me!!!

this in booking form




<?php

                        echo $form->dropDownList($model, 'shipper_id', CHtml::listData(Shipper::model()->findAll(), 'id', 'name'), array(

                                'prompt' => 'Select One',

                                'ajax' => array(

                                'type' => 'POST',

                                'dataType'=>'json',

                                'url' => CController::createUrl('Booking/clientFromThis'),

                        

                                'success' => 'function(data) {

                                $("#Work_order_client_view").html(data.client);

                                

                                }',

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

                        ),

                        ));

             ?>


 <div class="total_qty_lebel_custom">Client</div>

                        <div id="Work_order_client_view">

                        

                        </div>



this in my controller




public function actionClientFromThis() {

		 

        $data = Booking::model()->findAll('id=:shipper_id', array(':shipper_id' => $_POST['shipper_id']));

		

        foreach ($data as $d):

       		$detail= Shipper::model()->findByPk($d->id);

			

       			 $client='<b>Address 1: </b>'.$detail->address.'<br /><b>Address 2: </b>'.$detail->addr1.'<br /><b>Address 3: </b>'.$detail->addr2

				 						.'<br /><b>Tel: </b>'.$detail->tel.'<br /><b>Fax: </b>'.$detail->fax.'<br /><b>Email: </b>'.$detail->email;

				 

				

		

                	echo $array = CJSON::encode(array(

                		'client' => $client,

						

						

        			 ));

        endforeach;

		

        if (!$_POST['shipper_id']) {

                echo $array = CJSON::encode(array(

                'client' => '',

                ));

        }

    }



Please read carefully what you trying to post with ajax, shipper or shipper_id,

In here i noticed you posts shipper with ajax, but in your controller’s action , you want to capture shipper_id ,

please check again your code. Thanks

Hello tanimgt!! Plz help me again!!

I’m read it again and again but still not work for me!! :(

this is my code view/_form




<?php

             echo $form->dropDownList($model, 'shipper_id', CHtml::listData(Shipper::model()->findAll(), 'id', 'name'), array(

                                'prompt' => 'Select One',

                                'ajax' => array(

                                'type' => 'POST',

                                'dataType'=>'json',

                                'url' => CController::createUrl('Booking/ShipperDetail'),

                        

                                'success' => 'function(data) {

                                $("#Work_order_client_view").html(data.client);

                                

                                }',

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

                        ),

                        ));

                        ?>


 						<div class="view">Shipper Detail

                        	<div id="Work_order_client_view">

                        

                        	</div>

                        </div>



And this is my Booking controller




public function actionShipperDetail() 

	{

        $data = Booking::model()->findAll('id=:shipper_id', array(':shipper_id' => $_POST['shipper_id']));

        foreach ($data as $d):

        $client_name =  Shipper::model()->findByPk($d->id);

        $client=$client_name->address;

                echo $array = CJSON::encode(array(

                'client' => $client,

                ));

        endforeach;

        if (!$_POST['shipper_id']) {

                echo $array = CJSON::encode(array(

                'client' => '',

                ));

        }

    }



thank you so much !!! I am nearly crazy coz of this problem!! :(

Check if u have given access to actionShipperDetail in your controller file,I tink this may be the issue.

if you are using Firefox then you can download and install firebug add-on which is very helpful while working with Ajax calls.You can check what request is being sent and response from server.

Now check that if the client info is sent from controller action with this




<?php

 			echo $form->dropDownList($model, 'shipper_id',  CHtml::listData(Shipper::model()->findAll(), 'id', 'name'), array(

                                'prompt' => 'Select One',

                                'ajax' => array(

                                'type' => 'POST',

                                'dataType'=>'json',

                                'url' => CController::createUrl('Booking/ShipperDetail'),

                        

                                'success' => 'function(data) {

                         	alert(data.client);  // << this is the line i am talking about

                                $("#Work_order_client_view").html(data.client);

                                

                                }',

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

                        ),

                        ));

                        ?>


 						<div class="view">Shipper Detail

                        	<div id="Work_order_client_view">

                        

                        	</div>

                        </div>