Saturday 25 November 2017

on the fly - Yii framework....Calculate total on fly

itemprop="text">

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,
));
?>

            class="row">

labelEx($model,'price');
?>
price;
?>




labelEx($model,'qty'); ?>
$form->textField($model,'qty'); ?>
$form->error($model,'qty');
?>



class="row">
labelEx($model,'total');
?>
textField($model,'total');
?>



endWidget();
?>





$(document).ready(function()
{
var myVar = '999';

$("#model_total").val(myVar);
});
'



Here
I updated my script to clarify what I need, so here I'm trying to pass a value of '999'
to 'Total' on the same form but this doesn't work. It might be that I'm missing/doing
something wrong.



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


itemprop="text">
class="normal">Answer





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());
});


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...