views:

115

answers:

1

Hi guys, I'm newbie in Yii framework and start learning on small web project so I got stuck on this position as how to solve it.

I have a 'Order_form' where a user has confirm his order and if want to change the 'Qty' so only this can change then bydoing this I need to change the Total value to be calculate on fly on same form if Qty got changed by user.

beginWidget('CActiveForm', array( 'id'=>'order-form', 'enableAjaxValidation'=>false, )); ?>

<div class="row">
        <?php echo $form->labelEx($model,'price'); ?>
        <?php echo $model->price; ?>
</div>

<div class="row">
        <?php echo $form->labelEx($model,'qty'); ?>
        <?php echo $form->textField($model,'qty'); ?>
        <?php echo $form->error($model,'qty'); ?>
</div>

<div class="row">
        <?php echo $form->labelEx($model,'total'); ?>
        <?php echo $form->textField($model,'total'); ?>
</div>

endWidget(); ?>

$(document).ready(function() { var myVar = '999'; $("#model_total").val(myVar); }); '

Here i updated my script to clarify more what I need, so here I'm trying to pass '999' value to 'Total' on same form but this doesnt work might be Im missing/doing some thing wrong.

I would also like other way to calculate Qty x Price and get the result from controller if this is possible if so please guide through code.

I hope to understand my question and look forward to hear from you guys soon.

Thanks in advance for your attention.

Khan

A: 

One reason the JS you have might not be working is: are you SURE the "total" field ID is "model_total"? Usually it's something like "Order_total". Look at your html source to verify (Firebug helps).

Anyway, some other suggestions:

A common way to resolve this order total problem is an "Update Cart" button, that POSTs the form back to the controller where the order total is calculated, and then the page rendered again with the new total.

Another way to do this would be to set up an onBlur live() (or delegate) listener that does one of two things (similar to what you are trying now):

(1) POST the form to the controller where the total is recalculated. Something like this (untested):

$("#order-form #Order_qty").live('blur',function() {
  $.ajax({
    'type':'POST',
    'data':$('#order-form').serialize(),
    'success':function(data){$('#Order_total').val(data)}
  });}
);

You will need to have something in your controller action like this:

if(isset($_POST['ajax']) && $_POST['ajax']==='order-form' && isset($_POST['Order'])) {
  // calculate new total based on the $_POST Model_qty field
  echo $new_total;
  Yii::app()->end();
}

(2) You could do the calculation totally in JS, and update it like you are trying to do now. Just remember you cannot update the order qty in the database this way, sine you are not hitting the controller.

$("#order-form #Order_qty").live('blur',function() {
  $('#Order_total').val($('#Order_price').val() * $('#Order_qty').val());
});
thaddeusmt
Hi thaddeusmt, Thanks for your reply and specially with solution but I also find out solution by Ajax on Yii forum. I really really appreciated your time for above codes. Have a good day.
Khan