使用codeigniter

时间:2017-06-14 05:31:53

标签: php html ajax codeigniter

我正在开发codeigniter项目,其中viwe页面有文本框,单选按钮和按钮。我从数据库中获取文本框值并从日期和日期手动输入然后单击保存按钮插入查询获取空值,因为在控制器中在索引函数中串行调用所有函数。在视图页面的每个事件上调用控制器方法的最佳解决方案是什么?

leave_appl_view.php

<form action="leave_apply_control/index" method="post" id="myForm">
    <div class="row">
        <div class="col-md-3">
            <div class="form-group label-floating">
                <label class="control-label">Emp ID</label>
                <?php if(!empty($emp_leave)) 
                { 
                    foreach ($emp_leave as $liv)
                    { ?>   
                        <input type="text" class="form-control" name="id" value="<?php echo $liv->employee_id ?>" >                        
            </div>
        </div>
        <div class="col-md-4">
            <div class="form-group label-floating">
                <label class="control-label">Employee Name</label>              
                <input type="text" class="form-control" name="empname" id="empname" value="<?php echo $liv->employee_name ?>" >
            </div>
        </div>  
        <div class="col-md-4">
            <div class="form-group label-floating">
                <label class="control-label">Email address</label>
                <input type="email" class="form-control" name="empmail" id="empmail" value="<?php echo $liv->email ?>" >
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4">
            <div class="form-group label-floating">
                <label class="control-label">Leave type: </label>
                <input type="radio"  id="half" name="type" onclick="Checkradiobutton()" value="half"> Half Day
                <input type="radio"  id="full" name="type" onclick="Checkbutton()" value="full" Checked> Full Day
            </div>
        </div>                                              
        <div class="col-md-3">
            <div class="form-group label-floating" id="frm_dt">
                <label class="control-label">From Date: </label>
                <input type="text" class="form-control" id="datepicker1" name="from"  ></p>
            </div>
        </div>
        <div class="col-md-3">
            <div class="form-group label-floating" id="to_dt">
                <label class="control-label">To Date: </label>
                <input type="text" class="form-control" id="datepicker2" name="to" ></p>
            </div>
        </div>      
    </div>                                                  
    <div class="row">
        <div class="col-md-3">                                                                      
            <div class="form-group label-floating">
                <label class="control-label">Balance Leaves</label>                                                 
                <input type="text" class="form-control" name="bal_leave" value="<?php echo $liv->balanced_leaves ?>" >
            </div>
<?php } 
}
?>
        </div>
        <div class="col-md-3">
            <div class="form-group label-floating">
                <label class="control-label">Applied Leaves</label>
                <input type="text" class="form-control" name="totaldays" id="totaldays" readonly>                                               
            </div>
        </div>
        <div class="col-md-3">
            <div class="form-group label-floating">
                <label class="control-label">Applied Date</label>
                <input type="text" class="form-control" name="todayDate" id="todayDate" value="<?php echo date("d/m/Y"); ?>" readonly>
            </div>
        </div>
    </div>                                              
    <div class="row">                                                                              
        <div class="col-md-7">
            <div class="form-group label-floating">
                <label class="control-label">Reason </label>
                <TEXTAREA class="form-control" id="reason" name="reason"> </TEXTAREA>
            </div>
        </div>
    </div>
    <button type="submit" class="btn btn-primary" id="submit" name="submit" onclick="<?php // echo base_url() ?>leave_apply_control/save_data">Apply</button> 
    <button type="reset" class="btn btn-primary" onclick="<?php echo base_url();?>leave_apply_control" >Reset</button>
</form>

leave_Apply_control.php

<?php
class leave_apply_control extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->library(array('session', 'form_validation'));
        $this->load->library('calendar');
        $this->load->database();
        $this->load->model('user_model');
        $this->load->helper('date');           
    }//contruct close

    function index()
    {      
        $id=$this->session->userdata('id');
        $data['foren_id'] = $this -> user_model -> get_emp_id($id);    
        foreach($data['foren_id'] as $fk)
        {           
            //echo "////control page fk is   ".$fk->emp_id;
        }               
        $data2['emp_leave'] = $this->user_model->get_leave_count($fk->emp_id);                              
        $this->load->view('leave_application',$data2);  
        $this->save_data();
    }

    public function save_data()
    {
        $fd = date('Y-m-d',strtotime($this->input->post('from')));
        $td = date('Y-m-d',strtotime($this->input->post('to')));
        $data = array(
            'employee_id' => $this->input->post('id'),
            'emp_name' => $this->input->post('empname'),
            'leave_from' => $fd,
            'leave_to' => $td,
            'no_of_days' => $this->input->post('totaldays'),
            'applied_date' => date('Y-m-d H:i:s'),
            'reason' => $this->input->post('reason'),
            'mail' => $this->input->post('empmail'),
            'leave_type' => $this->input->post('type'),
            'balance_leaves' => $this->input->post('bal_leave'),
            'status' => 1,
        );              
        $this->user_model->insert_leave($data);                     
    }
}

0 个答案:

没有答案