doujiang2812
2014-09-24 08:15
采纳率: 0%
浏览 75
已采纳

CodeIgniter处理使用Jquery添加的多输入字段

Ok i have i form adding data into db. I have single input field and button wich add more input fields on click. My question is how to grab in controller all inputs and send it to model. My code so far is: Jquery:

<script>
    $('a').click(function(e){


       $('#inp').append('<div><input class = "new_input" type=text name="name[]"/><a class="remove_field "href="#"> X</a><div><br/>');
       $('.remove_field').click( function(e){
         e.preventDefault();
         $(this).parent('div').remove();
       })
    });
</script>

form:

<?php
// Forma za unos podataka
echo  $this->session->flashdata('item');
echo '<h4>Unesite podatke</h4>';
echo '<div id="warning"></div>';

$att = array('name'=>'form','onsubmit'=>" return validation()");
echo form_open('admin/crud/adding/',$att);

echo form_label('Novi podatak:', 'input_data_info') . br() . br();
$data = array(
              'name'        => 'input_data_info',
              'id'          => 'input_data_info',
              'placeholder'       => 'Unestite podatke',
             );

echo form_input($data) . br() . br();
echo '<div id="inp"></div>';

echo "<a href='#'>".'Novi unos'."</a>" .br() .br();
echo form_submit('save', 'Snimi') . br() . br();
echo form_submit('add', 'Dodaj').br();


echo form_close();

?>

controller:

$input_data_info = (string)$this->input->post('input_data_info', TRUE);
             //model za dodavanje podataka 
            $this->load->model('Data');
            $query = $this->Data->add($input_data_info);  
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumao6048 2014-09-24 08:43
    已采纳

    The first input field name is 'input_data_info' and then JQuery adds input fields with 'name[ ]'

    As you want to pull the data from all the input fields, they , first need to have a common name.

    So, rename the input field that you have created initially in the form to name= 'name[]' that makes your code:

    $data = array(
              'name'        => 'name[]',
              'id'          => 'input_data_info',
              'placeholder'       => 'Unestite podatke',
             );
    

    Then in you controller use something like this:

    $all_input_data = $this->input->post('name');

    //the $all_input_data is an array containing all your input values.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题