Add Values Of 2 Dropdown List And Textfield Together Display Result In Another Textfield

Hello,

I have two dropDownLists and a textfield.

I want the total sum of values of the two dropDownLists plus the textfield value and have them display the result in another textfield.

How would can i go about this. Below is a bit of the form i have created.


      <tr>

        <td><?php echo $form->labelEx($model,'typeofpaper'); ?></td>

        <td><?php $data = CHtml::listData(PaperType::model()->findAll(),'id','title'); 

                  if(isset($_REQUEST['id']))

                  {

                      $sel = $_REQUEST['id'];

                  } else {

                      $sel =''; 

                  }

                echo $form->dropDownList($model, 'typeofpaper', $data, array('prompt'=>'Type of Paper','options'=>array($sel=>array('selected'=>true)))); 

                

            ?>

            <?php echo $form->error($model,'typeofpaper'); ?></td>

      </tr>

      

       <tr>

        <td><?php echo $form->labelEx($model,'deadline'); ?></td>

             

        <td><?php 	echo $form->dropDownList($model,'deadline',

			

		  array('3hrs'=> '3 hrs', '6hrs'=>'6 hrs', '12hrs'=>'12 hrs', '24hrs'=>'1 day', '48hrs'=>'2 days', '72hrs'=>'3 days',

				'96hrs'=>'4 days', '120hrs'=>'5 days', '144hrs'=>'6 days', '168hrs'=>'7 days', '172hrs'=>'8 days', '196hrs'=>'9 days',

				'220hrs'=>'10 days', '244hrs'=>'11 days', '268hrs'=>'12 days', '292hrs'=>'13 days',	'316hrs'=>'14 days', '340hrs'=>'15 days'),array('empty' => 'Select Urgency Level')); ?>

            <?php echo $form->error($model,'deadline'); ?> </td>

      </tr>

      <tr>

        <td><?php echo $form->labelEx($model,'nopages'); ?></td>

        <td><?php echo $form->textField($model,'nopages'); ?>

            <?php echo $form->error($model,'nopages'); ?></td>

      </tr> 

Do you want to do this dynamically, without first submitting your form? In that case you’re bound to using javascript.

Summing a few values is not that difficult, although I wonder how you are going to do calculations with the values in the dropdownlists that you specified in your code, since they’re not numeric.

Still,. assuming somehow you were going to use numeric values that can be summed, here goes… haven’t tested, so might be buggy, but it’s to get you pointed in a direction (not saying it’s the right one ;))




Yii::app()->clientScript->registerCoreScript('jquery');


$typeofpaperName = CHtml::activeName($model, 'typeofpaper');

$deadlineName = CHtml::activeName($model, 'deadline');

$nopagesName = CHtml::activeName($model, 'nopages');


$script = <<<EOT


function calculateTotal() 

{

    var value1 = parseInt($('select[name="$typeofpaperName"]').val(), 10);

    var value2 = parseInt($('select[name="$deadlineName"]').val(), 10);


    var summedValue = value1 + value2;


    $('input[name="$nopagesName"]').val(summedValue);

}


$('select[name="$typeofpaperName"]').on('change', calculateTotal);

$('select[name="$deadlineName"]').on('change', calculateTotal);


EOT;


Yii::app()->clientScript->registerScript('js-sum-total', $script);




Yes i want to do this dynamically, without first submitting the form? to give a clearer picture of what i intend to achieve. I have the following two tables for model PaperType & Urgency respectively.

Papertype

id, paper_ type

Table Urgency

id urgency paper_id cost

in the views _form I a have two dropdownlist and a textfield. So a user select paper type, then selects urgency and lastly enters number of pages, which triggers an Ajax call to a Controller action totalCost() which queries the PaperType and Urgency tables.

In the controller this is what i was trying to fiddle with.




 $data = PaperType::model->()findByAttributes(

                       'paper_type'=>$_POST['typeofpaper'])


 $data2 = Urgency::model->()findByAttributes(

                  'urgency'=>$_post['deadline'],

                  'paer_id'=>$data->id

));




 $cost = $data2->cost;


 $nopages  = $_post['nopages'];


echo $total = $cost * $nopages;




Part of my views code.


 <tr>

        <td><?php echo $form->labelEx($model,'typeofpaper'); ?></td>

        <td><?php $data = CHtml::listData(PaperType::model()->findAll(),'title','title'); 

                  if(isset($_REQUEST['title']))

                  {

                      $sel = $_REQUEST['title'];

                  } else {

                      $sel =''; 

                  }

                echo $form->dropDownList($model, 'typeofpaper', $data, array('prompt'=>'Type of Paper','options'=>array($sel=>array('selected'=>true)))); 

                

            ?>

            <?php echo $form->error($model,'typeofpaper'); ?></td>

      </tr>

      

       <tr>

        <td><?php echo $form->labelEx($model,'deadline'); ?></td>

             

        <td><?php 	echo $form->dropDownList($model,'deadline',

			

		  array('3hrs'=> '3 hrs', '6hrs'=>'6 hrs', '12hrs'=>'12 hrs', '24hrs'=>'1 day', '48hrs'=>'2 days', '72hrs'=>'3 days',

				'96hrs'=>'4 days', '120hrs'=>'5 days', '144hrs'=>'6 days', '168hrs'=>'7 days', '172hrs'=>'8 days', '196hrs'=>'9 days',

				'220hrs'=>'10 days', '244hrs'=>'11 days', '268hrs'=>'12 days', '292hrs'=>'13 days',	'316hrs'=>'14 days', '340hrs'=>'15 days'),array('empty' => 'Select Urgency Level')); ?>

            <?php echo $form->error($model,'deadline'); ?> </td>

      </tr>

      <tr>

        <td><?php echo $form->labelEx($model,'nopages'); ?></td>

        <td><?php echo $form->textField($model,'nopages', array('id'=>'total',

							  'ajax' =>array(

							  'type' =>'POST',

							  'url '=> cController::createUrl('orders/totalcost'),

						      'update'=>'#totals',

							  )

						   )); ?>

            <?php echo $form->error($model,'nopages'); ?></td>

      </tr> 

I want the result display in another text field. My solution doesn’t work as is. I need help on this one.

Ok, well the $_POST var in your controller action is not going to contain the keys $_POST[‘typeofpaper’] and $_POST[‘deadlines’], because judging by your view code, you’re using ActiveForm.

In that case, the values will be in $_POST[‘YourModelsName’][‘typeofpaper’], $_POST[‘YourModelsName’][‘deadlines’] and $_POST[‘YourModelsName’][‘nopages’] (TIP: use CVarDumper::dump($_POST) or PHP’s var_dump($_POST))

The easiest way to handle these posts is to just dump them in the model’s attribute variable. If you have rules defined for these attributes in your model, they will take the values from the POST by mass assignment.

Example (in your controller action):




$model = new YourModelsName();

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


$data = PaperType::model()->findByAttributes(array('paper_type'=>$model->typeofpaper));

$data2 = Urgency::model()->findByAttributes(array('urgency'=>$model->deadline,'paer_id'=>$data->id));


$cost = $data2->cost;

$nopages  = $model->nopages;

$total = $cost * $nopages


echo $total;



Does that help?

I am very optimistic we are on the right track. When i the complete the form, now i get the view.php overlapping the form. How should my _form look like at this point?.

Presently this is what i have


<tr>

        <td><?php echo $form->labelEx($model,'typeofpaper'); ?></td>

        <td><?php $data = CHtml::listData(PaperType::model()->findAll(),'title','title'); 

                  if(isset($_REQUEST['title']))

                  {

                      $sel = $_REQUEST['title'];

                  } else {

                      $sel =''; 

                  }

                echo $form->dropDownList($model, 'typeofpaper', $data, array('prompt'=>'Type of Paper','options'=>array($sel=>array('selected'=>true)))); 

                

            ?>

            <?php echo $form->error($model,'typeofpaper'); ?></td>

      </tr>

      

       <tr>

        <td><?php echo $form->labelEx($model,'deadline'); ?></td>

             

        <td><?php 	echo $form->dropDownList($model,'deadline',

			

		  array('3hrs'=> '3 hrs', '6hrs'=>'6 hrs', '12hrs'=>'12 hrs', '24hrs'=>'1 day', '48hrs'=>'2 days', '72hrs'=>'3 days',

				'96hrs'=>'4 days', '120hrs'=>'5 days', '144hrs'=>'6 days', '168hrs'=>'7 days', '172hrs'=>'8 days', '196hrs'=>'9 days',

				'220hrs'=>'10 days', '244hrs'=>'11 days', '268hrs'=>'12 days', '292hrs'=>'13 days',	'316hrs'=>'14 days', '340hrs'=>'15 days'),array('empty' => 'Select Urgency Level')); ?>

            <?php echo $form->error($model,'deadline'); ?> </td>

      </tr>

      <tr>

        <td><?php echo $form->labelEx($model,'nopages'); ?></td>

        <td><?php echo $form->textField($model,'nopages', array('id'=>'total',

							  'ajax' =>array(

							  'type' =>'POST',

							  'url '=> cController::createUrl('orders/totalcost'),

						      'update'=>'#totals',

							  )

						   )); ?>

            <?php echo $form->error($model,'nopages'); ?></td>

      </tr> 

      

      <tr>

        <td><?php echo $form->labelEx($model,'priceperpage'); ?></td>

        <td><?php echo $form->textField($model,'priceperpage',array('size'=>12,'maxlength'=>12)); ?>

            <?php echo $form->error($model,'priceperpage'); ?></td>

      </tr>

      

      <tr>

       <td>Can you see it</td>

        <td><div id="totals"> </div></td>

       

      </tr> 

I want the result to show in the <div id="totals"> HERE !!!!!!!!</div>

Is the ajax controller action calling $this->render() somewhere? If it is, it will render and return the entire page (including all <html><head></head>,etc… and place it in your totals div. If that’s the case you should replace the render call with renderPartial or just echo and exit.

Use firefox in combination with firebug, or Google Chrome and F12 (or right click and click inspect element) to see how your source changed by your ajax request. This might help you debug your problem.

If that doesn’t help. Can you post your ajax controller action code as well?

Hi sorry i was on honeymoon trip :rolleyes: but i am back. :D

he is my Ajax controller action




public function actionTotalCost() {

		$model = new Orders();

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

		

		$data = PaperType::model()->findByAttributes(array('title'=>$model->typeofpaper));

		$data2 = Urgency::model()->findByAttributes(array('urgency'=>$model->deadline,'paper_id'=>$data->id));

		$cost = $data2->cost;

		$nopages  = $_post['nopages'];


		$nopages  = $model->nopages;

		$total = $cost * $nopages;


		echo $total; 

}



Who will come to my rescue.

Replying to Add Values Of 2 Dropdown List And Textfield Together Display Result In Another Textfield