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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀