Using Jquery To Compute And Assign Value To Textfield

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.

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

thank you for the effort but it’s still not working.

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

It worked! thanks.

glad could help you

‘rules’=>array(

                    'po/local/requestDetails/computePrice'=&gt;'site/computePrice',


                            '&lt;controller:&#092;w+&gt;/&lt;id:&#092;d+&gt;'=&gt;'&lt;controller&gt;/view',


                            '&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;/&lt;id:&#092;d+&gt;'=&gt;'&lt;controller&gt;/&lt;action&gt;',


                            '&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;'=&gt;'&lt;controller&gt;/&lt;action&gt;',


                    ),

Hello Wer do i add this code plz explain in detail thank you in advnce…!!!

In your config file, under the URL Manager component