我有三列。两列的产品使用codeigniter验证规则进入第三列名称income_amount。第一列是crop_quantity,第二列是per_rate
控制器
$this->form_validation->set_rules('crop_quantity', 'Crop Quantity', 'required|numeric');
$this->form_validation->set_rules('per_rate', 'Per Rate', 'required|numeric|callback_get_product');
$this->form_validation->set_rules('income_amount', 'Income Amount', 'required|numeric');
$this->form_validation->set_rules('select_client', 'Client Name', 'required');
function get_product($crop_quantity,$per_rate)
{
$q = $crop_quantity;
$p = $per_rate;
if(q!='' && p!='')
{
$total = $p * $q;
$income_amount=$total;
}
}
查看
<div class="control-group">
<label class="control-label">Crop Quantity</label>
<div class="controls">
<input type="text" class="span6 m-wrap" value="<?echo $name->crop_quantity?>" name="crop_quantity" >
<?php echo form_error('crop_quantity'); ?>
</div>
</div>
答案 0 :(得分:1)
将以下代码添加到Controller Post:
$this->form_validation->set_rules('crop_quantity', 'Crop Quantity', 'required|numeric');
$this->form_validation->set_rules('per_rate', 'Per Rate', 'required|numeric|callback_get_product');
$this->form_validation->set_rules('income_amount', 'Income Amount', 'required|numeric|callback_validate_amount');
$this->form_validation->set_rules('select_client', 'Client Name', 'required');
在同一控制器中添加名为'validate_amount'的新函数(提示:callback_validate_amount)
function validate_amount() {
$q = $this->input->post('crop_quantity');
$p = $this->input->post('per_rate');
$income_amount = $this->input->post('income_amount');
$total = $p * $q;
if ($income_amount == $total) {
return TRUE;
} else {
$this->form_validation->set_message('validate_amount', 'The %s field must contain the product of crop_quantity and per_rate');
return FALSE;
}
}
答案 1 :(得分:0)
您需要对控制器执行任何操作。将此更改添加到
<script>
function calculate() {
var myBox1 = document.getElementById('crop_quantity').value;
var myBox2 = document.getElementById('per_rate').value;
var result = document.getElementById('income_amount');
var myResult = myBox1 * myBox2;
result.value = myResult;
}
window.onload = calculate();
</script>
<div class="control-group">
<label class="control-label">Crop Quantity</label>
<div class="controls">
<input type="text" id="crop_quantity" oninput="calculate()" class="span6 m-wrap" value="<?echo $name->crop_quantity?>" name="crop_quantity" >
<?php echo form_error('crop_quantity'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Per Rate</label>
<div class="controls">
<input type="text" id="per_rate" class="span6 m-wrap" value="<?echo $name->per_rate ?>" name="per_rate" oninput="calculate()">
<?php echo form_error('per_rate'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Income Amount</label>
<div class="controls">
<input type="text" id="income_amount" class="span6 m-wrap" value="<?echo $name->income_amount?>" name="income_amount" >
<?php echo form_error('income_amount'); ?>
</div>
</div>
注意:我在每个输入字段中添加了属性id
。您还可以在输出框中添加禁用字段
注2:我也在输入字段中使用了oninput="calculate()"
。示例
function calculate() {
var myBox1 = document.getElementById('crop_quantity').value;
var myBox2 = document.getElementById('per_rate').value;
var result = document.getElementById('income_amount');
var myResult = myBox1 * myBox2;
result.value = myResult;
}
window.onload = calculate();
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<table width="80%" border="0">
<tr>
<th>crop_quantity</th>
<th>per_rate</th>
<th>income_amount</th>
</tr>
<tr>
<td><input id="crop_quantity" name="crop_quantity" type="text" oninput="calculate()" value="13" /></td>
<td><input id="per_rate" name="per_rate" value="10" type="text" oninput="calculate()" /></td>
<td><input id="income_amount" name="income_amount" disabled/></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
&#13;
修改更新javascript
答案 2 :(得分:0)
1.请检查<?(shorthand tags) are enabled or not else use <?php instead of ?>
2.更多的事情是<? and echo