简体   繁体   中英

How to insert zero rows in codeigniter

I'm click my submit button freely without enter values in the form..then in my database a row inserted that columns contain zeros

controller

function submit_expense()
{
    $exp_date=$this->input->post('expense_date');
    $expense_ids= $this->input->post('expense');
    $expense_ids=substr($expense_ids,0,strlen($expense_ids)-1);
    $expense_id_array = explode(',',$expense_ids);
    $id=$this->session->userdata('userid');
    for($i=0;$i<count($expense_id_array);$i++)
    {
        $required_id = TRIM($expense_id_array[$i]);
        $this->form_validation->set_error_delimiters('<div style="color:#B94A48">', '</div>');                                 
        $this->form_validation->set_rules('exp_amount_'.$required_id, 'Expense Amount', 'required|numeric');
        $this->form_validation->set_rules('comment_'.$required_id, 'Comments', 'required|alpha');
        if ( $this -> form_validation -> run() === FALSE )
        {
            $this->index();
        }
        else
        {
            $amount= $this->input->post('exp_amount_'.$required_id);
            $comment=$this->input->post('comment_'.$required_id);
            $expense_data=array(
                'expense_id'=>$required_id,
                'expense_amount'=>$amount,
                'user_id'=>$id,
                'date'=>$exp_date,
                'comments'=>$comment,
            );
            $this->home_model->insert_expense($expense_data);
            $this->session->set_flashdata('message', 'Successfully submitted.');
            redirect(base_url().'home/index');
        }
    }
}

And also my validation is not working

footer_view

var new_String ='<div class="form-group" id="ex_'+unqid1+'">'
    +'<label for="exampleInputPassword1">'+name+'</label> </br>'
    +'<input name="exp_amount_'+unqid1+'" id="id_'+unqid1+'" type="text" '+
    'class="form-control" placeholder="Enter '+name+' expense amount" style="margin-right:20px;" required>'
    +'<input name="comment_'+unqid1+'" type="text" id="comment"  class="form-con" placeholder="Comments" style="margin-right:20px;" required ></div >' ;
$("#create_exp").append(new_String);

view

this is my view use the id create_exp in my footer

<form role="form"  action="<?echo base_url()?>home/submit_expense" method="post">
    <?$todays_date=date('Y-m-d');?>
    <input id="expense_date" name="expense_date" value="<?echo $todays_date;?>" type="hidden" />
    <div class="red">
        <? echo $this->session->flashdata('message'); ?>
    </div>

    <div class="box-body" id="create_exp">
        <?php echo validation_errors(); ?>
        <span>Choose from the right which all you need to enter for today</span>
        <!--here goes the input boxes-->
        <!-- /.input boxes -->
    </div><!-- /.box-body -->
    <span id="errmsg1"></span>
    <input id="expenseVal" name="expense" type="hidden"   class="form-control">
    <div class="box-footer" id="button_id">
        <button type="submit" class="btn btn-primary" >Submit</button>
    </div>
</form>

As the OP Wants to show the all the Errors inside a div

<div id="error"> 
<?php echo validation_errors(); ?> 
</div>

As the OP Wants some additional info,

To include a view or common page

$this->load->view('yourownpage');

Then you should have yourownpage.php inside your views folder

To have pagination, Have this in your controller's function

$this->load->library('pagination'); 
$config['base_url'] = 'yourownpage'; 
$config['total_rows'] = 200; 
$config['per_page'] = 20; 
$this->pagination->initialize($config); 
echo $this->pagination->create_links();

Note :

Make sure to place the error div in the place that didn't hurt your textbox

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM