0

Im trying to add dtnamic fileds and through that insert arrays data into mysql but when i try this code nothing error and ref no only inserting can anyone help me . im using Codeigniter framework.

This is my view page : my form with js

<div id="invoice_form">
        <?php echo form_open(base_url().'sample/myinvoice'); ?>
        <div><?php echo form_input(array('id'=>'ref','name'=>'ref'));?></div>
            <div  id="description"><p id="add_field">ADD DESCRIPTION</p></div>
        <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Save" />
        <?php echo form_close(); ?>
        </div>

This is my Controller :

function myinvoice()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('ref', 'REFERENCE NO', 'trim');
        $this->form_validation->set_rules('description', 'DESCRIPTIONS', 'trim'); 
        $this->form_validation->set_rules('voucher_no', 'DESCRIPTIONS', 'trim'); 
        $this->form_validation->set_rules('price', 'DESCRIPTIONS', 'trim'); 

        if($this->form_validation->run())
        {
            $this->load->database();
            $this->load->model('sample_model');

            $ref =  $this->input->post('ref');
            $description = $this->input->post('description');
            $voucher_no = $this->input->post('voucher_no');
            $price = $this->input->post('price');
             $i = 0;
             if($description){
            foreach($description as $row){
            $data['ref_no'] = $ref[$i];
            $data['descriptions'] = $description[$i];
            $data['voucher'] = $voucher_no[$i];
            $data['value'] = $price[$i];
             $this->db->insert("description",$data);
            $i++; }}
        }else{
            echo "form validation error";}}
3
  • possible duplicate of How to Insert Arrays (Multiple rows) into Mysql using Codeigniter? Commented Aug 5, 2015 at 10:27
  • that is 3 table this is only one table but same thing i want insert can u help me Commented Aug 5, 2015 at 10:34
  • chek below me and Santy conversation plz Commented Aug 5, 2015 at 10:36

1 Answer 1

0

This is the answer i hope this will help to many people :)

This is my view Page:

    <div id="invoice_form">
    <?php echo form_open(base_url().'sample/myinvoice'); ?>
    <div><?php echo form_input(array('id'=>'ref','name'=>'ref'));?></div>
    <div  id="description"><p id="add_field">ADD DESCRIPTION</p></div>
    <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Save" />
    <?php echo form_close(); ?>
    </div>
    <script>
    var count = 0;
    $(document).ready(function() {
    $('p#add_field').click(function(){
       count += 1;
       var html='<strong>Description  '+ count +'</strong>'+'<input id="description'+ count +'"name="description[]'+'" type="text" />'+'<input id="description'+ count +'"name="voucher_no[]'+'" type="text" />'+'<input id="description'+ count +'"name="price[]'+'" type="text" /><br />';
       $('#description').append(html); });
       });
    </script>

This is My Controller :

function myinvoice()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('ref', 'REFERENCE NO', 'trim');
        $this->form_validation->set_rules('description[]', 'DESCRIPTIONS', 'trim'); 
        $this->form_validation->set_rules('voucher_no[]', 'DESCRIPTIONS', 'trim'); 
        $this->form_validation->set_rules('price[]', 'DESCRIPTIONS', 'trim'); 

        if($this->form_validation->run())
        {
            $this->load->database();
            $this->load->model('sample_model');

            $ref =  $this->input->post('ref');
            $description = $this->input->post('description');
            $voucher_no = $this->input->post('voucher_no');
            $price = $this->input->post('price');
             $i = 0;
             if($description){
            foreach($description as $row)
            {
            $data['ref_no'] = $ref;
            $data['descriptions'] = $description[$i];
            $data['voucher'] = $voucher_no[$i];
            $data['value'] = $price[$i];
             $this->db->insert("description",$data);
            $i++;
            }}
            }else{
            print_r(validation_errors());
            }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.