coy_coy
(Rexliterato)
1
I wanted to compute the price on the item based on the price listed on the database every time quantity textfield changes.
view javascript
jQuery("#RequestDetails_quantity").change(function computePrice(event){
e.preventDefault();
<?php echo CHtml::ajax(array(
'url'=>array('//po/local/requestDetails/computePrice','id'=>$childModel->item_id),
'data'=> "js:$(this).serialize()",
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
var total = price * (\"#RequestDetails_quantity\").val();
if (data.status == 'failure')
{
alert('Failed to cancel ' + data.request_number + '.');
} else {
$(\"#RequestDetails_price\").val(total);
}
} ",
))?>
return false;
});
controller
public function actionComputePrice($id) {
$result = array();
if (isset($id)) {
$itemModel = Item::model()->FindByPk($id);
if ($itemModel !== null) {
$result = array(
'status'=>'success',
'price'=>$itemModel->price,
);
}else {
$result = array(
'status'=>'failure',
'id'=>$id,
);
}
}
echo CJSON::encode($result);
}
any thoughts on this issue? any help is greatly appreciated. Thanks.
alirz23
(Ali Raza)
2
here is a re factored version of your javasript
jQuery("#RequestDetails_quantity").change(function computePrice(event){
$.ajax({
url: <?php echo CController::createUrl('//po/local/requestDetails/computePrice','id'=>$childModel->item_id); ?>,
data: $(this).serialize(),
type: "POST",
dataType: "json",
sucess: function(data) {
var total = price * $("#RequestDetails_quantity").val();
if (data.status == 'failure')
{
alert('Failed to cancel ' + data.request_number + '.');
} else {
$("#RequestDetails_price").val(total);
}
}
});
e.preventDefault();
});
you dont have to return false e.prevenDefault() does the very same thing
coy_coy
(Rexliterato)
3
thank you for the effort but it’s still not working.
alirz23
(Ali Raza)
4
I did not know that you were having problem I thought you were asking for review here it is
// my controller
public function actionComputePrice()
{
$result = array("total" => 10 + $_POST["qty"], "status"=>"OK");
echo CJSON::encode($result);
}
// my routes under urlManger
'rules'=>array(
'po/local/requestDetails/computePrice'=>'site/computePrice',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
// my view
<?php Yii::app()->clientScript->registerCoreScript("jquery"); ?>
<script type="text/javascript">
$(document).ready(function(){
$("#RequestDetails_quantity").change(function(){
$.ajax({
url: "<?php echo CController::createUrl('/po/local/requestDetails/computePrice'); ?>",
data: $(this).serialize(),
type: "post",
dataType: "json",
success: function(data) {
if (data.status === 'failure') {
$('#new_price').val('Error request failed.');
} else {
$("#new_price").html(data.total);
}
}
});
});
});
</script>
EDIT:
I tested the code it works
Dhananjay
(Dhananajay Modagekar)
7
‘rules’=>array(
'po/local/requestDetails/computePrice'=>'site/computePrice',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
Hello Wer do i add this code plz explain in detail thank you in advnce…!!!
georaldc
(Georaldc)
8
In your config file, under the URL Manager component