I want to subtract an input field value from a database value how to manage that?? what changes do i need in the MVC ?? my code in myform.ctp as follows:
echo $this->Form->input('discount', array('label' => 'Discount'));
$val1 = //---------here the input value ---------//
$val2 = $this->data['Product']['price']; //value from database
$val3 = $val2 - $val1;
echo $val3;
after editing :
<?php
echo $this->Form->input('discount', array('id'=>'n','label' => 'Discount %','placeholder'=>'Value in %'));
echo $this->Form->hidden('price', array('id'=>'p'));
echo $this->Form->input('finalprice', array('id'=>'c'));
?>
<script>
var a=document.getElementById('n').value;
document.write(a);
var b=document.getElementById('p').value;
document.write(b);
var c=b-a;
document.getElementById('c').value=c;
document.write(c);
</script>
now im getting the inputs values, made the operation , but i need to insert the final value into database field... data['Product']['finalprice'] .........how to do that?
First set the value at controller
$this->set('val1',$this->data['Product']['price']);
Then In view
<input type="hidden" value="<?php echo $val1; ?>" id="val1">
<?php echo $this->Form->input('discount', array('label' => 'Discount','id'=>'val2')); ?>
<script>
$(document).ready(function(){
var finalval=parseInt($('#val1').val())-parseInt($('#val2').val());
alert(finalval);
});
</script>
Following code wrie in your Product
Model which is inbuilt functionality of cakephp
public function beforeSave($options = array()) {
if (!empty($this->data['Product']['discount']) &&
!empty($this->data['Product']['price'])
) {
$this->data['Product']['finalprice'] =
$this->data['Product']['price'] - $this->data['Product']['discount'];
}
return true;
}
Check Manual for beforeSave
function in cakephp.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.