在输入表单中的值并使用codeigniter插入数据库后获取总值

时间:2016-11-17 13:10:32

标签: php mysql codeigniter

如果我从dorpdown中选择本地销售并输入DEF,GHI值那么DEF,GHI的总和应该显示在总价值中,或者如果我从下拉列表中选择Inter State,Stock Transfers,那么如果我们输入ABC值那么该值应该是以总值显示,否则如果我们从下拉列表中选择JOB WORK,EXEMPTED SALES,则总值应显示为零。应该将我们获得的总值插入数据库中。

控制器:

function addinvoice()
{
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
    $this->form_validation->set_rules('user','User');
    $this->form_validation->set_rules('freight_charges');
    $this->form_validation->set_rules('abc');
    $this->form_validation->set_rules('def');
    $this->form_validation->set_rules('ghi');
    $this->form_validation->set_rules('total');
    if($this->form_validation->run()== FALSE)   
    {       
    $data['mainpage']='invoice';
    $data['mode']='add';
    $this->load->view('templates/template',$data);
    }
    else
    {
        $this -> invoice_model -> insert();
        $this->flash->success('<h2> Details added Successfully!</h2>');
        redirect('invoice');
    }
}

型号:

function insert()
{
$data['total']=0;
$data['user'] = $this->input->post('user');
$data['ghi'] = ($this->input->post('ghi'))?$this->input->post('ghi'):0;
$data['abc'] = ($this->input->post('abc'))?$this->input->post('abc'):0;
$data['def'] = ($this->input->post('def'))?$this->input->post('def'):0;
$data['total'] = $data['ghi'] + $data['abc'] + $data['def'];
$data['freight_charges'] = $this->input->post('freight_charges');
$this->db->insert('invoice',$data);
}

查看:

<script>
function showRequiredOption(cval)
{
if((cval=='interstate') || (cval == "stocktransfers"))
{
    $('#ghi').hide();
    $('#def').hide();
    $('#abc').show();
}
else if ((cval=='exemptedsales') || (cval=="zeroratedsales") ||(cval=="jobwork"))
{
    $('#ghi').hide();
    $('#def').hide();
    $('#abc').hide();

}
else
{
    $('#abc').hide();
    $('#ghi').show();
    $('#def').show();

}
}
</script>
<div class="col-md-9 col-md-offset-2">
<div id="legend">
<legend class="">Profile Information</legend>
</div>  
<form role="form" action="<?php echo site_url();?>invoice/addinvoice" method="post" class="form-horizontal" id="location" method="post" accept-charset="utf-8">     
<div class="form-group">
    <label class="control-label col-sm-2 " for="user">User</label> 
    <div class="col-sm-4 col-sm-offset-1">
        <select id="user" name="user" onchange="showRequiredOption(this.value)">
            <option value="employee">Local Sales</option>
            <option value="interstate">Inter state</option>
            <option value="stocktransfers">Stock transfers</option>
            <option value="exemptedsales">Exempted Sales</option>
            <option value="zeroratedcompany">Zero Rated Sales</option>
            <option value="jobwork">Job Work</option>
        </select>
    </div>
</div>      
<div class="form-group">
  <label class="control-label col-sm-2 " for="freight_charges">Freight Charges</label>
  <div class="col-sm-4 col-sm-offset-1">
    <input type="text" class="form-control" id="freight_charges" name="freight_charges" value="<?php echo set_value('freight_charges');?>"  />
  </div>
</div>

<div class="form-group" id="abc" style="display:none;">
  <label class="control-label col-sm-2 " for="abc">ABC</label>
  <div class="col-sm-4 col-sm-offset-1">
    <input type="text" class="form-control" id="abc" name="abc" value="<?php echo set_value('abc');?>"/ >
  </div>
</div>  
 <div class="form-group" id="def">
  <label class="control-label col-sm-2 " for="def">DEF </label>
  <div class="col-sm-4 col-sm-offset-1">
    <input type="text" class="form-control" id="def" name="def" value="<?php echo set_value('def');?>"/ >
  </div>
</div>  
<div class="form-group" id="ghi">
  <label class="control-label col-sm-2 " for="ghi">GHI</label>
  <div class="col-sm-4 col-sm-offset-1">
    <input type="text" class="form-control" id="ghi" name="ghi" value="<?php echo set_value('ghi');?>"/  >
  </div>
</div> 
  <div class="form-group" id="cgst">
  <label class="control-label col-sm-2 " for="total">Total</label>
  <div class="col-sm-4 col-sm-offset-1">
    <input type="text" class="form-control"  name="total"   >
  </div>
</div>      
 <button id="submit" type="submit" class="btn" name="submit">Submit</button>
 </form>
 </div>

无论我从下拉列表中选择哪些值,只有那些要插入数据库的值,其余值应该在数据库中插入为零。

实际上我没有得到如何做到这些可以任何人检查这个。谢谢提前。

2 个答案:

答案 0 :(得分:0)

你应该在php代码中检查发布的值为:

$total = 0;
if($this->input->post('this')){
     $total = $this->input->post('this');//value in total
}
if($this->input->post('this2')){
     $total += $this->input->post('this2');//value in total
}

并且最后在db中发送$ total值。 简而言之,如果是其他标签,你可以设置变量,如;

$this = ($this->input->post('this'))?$this->input->post('this'):0;
$this2 = ($this->input->post('this2'))?$this->input->post('this2'):0;

然后最后你可以将它们总计并保存到数据库中。或者如上面建议的那样,在您的表中将列设为DEFAULT 0。

-------在你的情况下------

function insert()
{
    $data['total']=0;
    $data['user'] = $this->input->post('user');
    $data['ghi'] = ($this->input->post('ghi'))?$this->input->post('ghi'):0;
    $data['abc'] = ($this->input->post('abc'))?$this->input->post('abc'):0;
    $data['def'] = ($this->input->post('def'))?$this->input->post('def'):0;
    $data['total'] = $data['ghi'] + $data['abc'] + $data['def'];
    $data['freight_charges'] = $this->input->post('freight_charges');
    $this->db->insert('invoice',$data);
}

---------------在JavaScript中------------ 在你的事件处理程序上,你可以通过他们的ID来总结这些。

var total = parseInt($('#ghi').val())+parseInt($('#def').val());

然后在您的总div中显示此总数

$('#yourTotalDiv').text(total);

答案 1 :(得分:0)

在输入详细信息时显示总金额。

mongoose.connect("mongodb://localhost/blog-react");

app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.json()); // <--- Here
app.use(bodyParser.urlencoded({extended: true}));