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

标签 php mysql codeigniter

如果我从 dorpdown 中选择 Local Sales 并输入 DEF、GHI 值,那么 DEF、GHI 的总和应该显示在总值中,或者如果我从下拉列表中选择 Inter State、Stock Transfers,那么如果我们输入 ABC 值,该值应该是以总值(value)显示,否则如果我们从下拉列表中选择 JOB WORK,EXEMPTED SALES,则总值(value)应显示为零。我们得到的总值(value)应该被插入到数据库中。

Controller :

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>

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

实际上我不知道如何做这些任何人都可以检查这个。在此先感谢。

最佳答案

您应该检查您的 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 值。 简而言之 php if else 标签你可以设置变量;

$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());

然后在你的total div中显示这个总数

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

关于php - 在表单中输入值并使用 codeigniter 插入数据库后获取总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40655820/

相关文章:

php - codeigniter 中可以进行多个单词搜索吗?

php - 具有结构化内容的单个 PHP 页面的模板引擎?

php - 在 PHP 中实现分页

javascript - chartjs 不工作 php 页面但在 html 页面上工作

mysql - 带有 MySQL 连接的 ember.js

sql - 连接更新的 MySQL 语法

php - 如何使用 Laravel Scheduler 命令将输出重定向到 STDOUT?

mysql - 在 mysql 中按月重复客户计数

mysql - 获取mysql字段的总和并传递总和以在codeigniter中查看

php - 创建自定义 codeigniter 验证规则