最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CodeIgniter dynamic input with jquery and insert to database - Stack Overflow

programmeradmin1浏览0评论

I stumbled upon a concern regarding my friends CI project: he wanted to add a new <input> tag to the body and insert to MySQL, so I made this code:

<?php echo form_open('wele');?>
<div id="container">
    <p id="add_field"><a href="#"><span>&raquo; Add Educational Background.....</span></a></p>
</div>
<div class="spacer"></div>
<input id="go" name="btnSubmit" type="submit" value="insert" class="btn" />
<?php echo form_close();?>

Controller:

$this->load->view('wele_message');
$this->load->model('test'); 

if($this->input->post('attain',true) != null){

    foreach ($this->input->post('attain',true) as $a) {
        foreach ($this->input->post('major',true) as $m) 
            foreach ($this->input->post('school',true) as $s) 

                $data = array(
                    'attain'=>$a,
                    'major'=>$m,
                    'school'=>$s);
                $this->db->insert('stress',$data);

                print_r($data); // for show purposes

                redirect(base_url());

            }

        }

the Javascript:

<script type="text/javascript">
var count = 0;
$(function(){
    $('p#add_field').click(function(){
        count += 1;
        $('#container').append(
            '<input id="major' + count + '" name="attain[]' + '" type="text" />' + 
            '<input id="major' + count + '" name="major[]' + '" type="text" />' + 
            '<input id="' + count + '" name="school[]' + '" type="text" /><br />' );

    });
});
</script> 

This was written in CodeIgniter.

I stumbled upon a concern regarding my friends CI project: he wanted to add a new <input> tag to the body and insert to MySQL, so I made this code:

<?php echo form_open('wele');?>
<div id="container">
    <p id="add_field"><a href="#"><span>&raquo; Add Educational Background.....</span></a></p>
</div>
<div class="spacer"></div>
<input id="go" name="btnSubmit" type="submit" value="insert" class="btn" />
<?php echo form_close();?>

Controller:

$this->load->view('wele_message');
$this->load->model('test'); 

if($this->input->post('attain',true) != null){

    foreach ($this->input->post('attain',true) as $a) {
        foreach ($this->input->post('major',true) as $m) 
            foreach ($this->input->post('school',true) as $s) 

                $data = array(
                    'attain'=>$a,
                    'major'=>$m,
                    'school'=>$s);
                $this->db->insert('stress',$data);

                print_r($data); // for show purposes

                redirect(base_url());

            }

        }

the Javascript:

<script type="text/javascript">
var count = 0;
$(function(){
    $('p#add_field').click(function(){
        count += 1;
        $('#container').append(
            '<input id="major' + count + '" name="attain[]' + '" type="text" />' + 
            '<input id="major' + count + '" name="major[]' + '" type="text" />' + 
            '<input id="' + count + '" name="school[]' + '" type="text" /><br />' );

    });
});
</script> 

This was written in CodeIgniter.

Share Improve this question edited Sep 26, 2015 at 2:16 Jonathan Lam 17.4k17 gold badges71 silver badges99 bronze badges asked Sep 26, 2015 at 1:19 GODZGODZ 1151 silver badge12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

There are too many loops nested inside each other which would result in far too many database entries.

Also the redirect was inside the loop so as soon as the first insert was made the redirect would occur

Try:

if ($this->input->post('attain')) { // returns false if no property 
    $attain = $this->input->post('attain', true);
    $schools = $this->input->post('school', true);
    $major = $this->input->post('major', true);

    foreach ($attain as $i => $a) { // need index to match other properties
        $data = array(
            'attain' => $a,
            'major' => isset($majors[$i]) ? $majors[$i] : '',
            'school' => isset($schools[$i]) ? $schools[$i] : ''
        );

        if (!$this->db->insert('stress', $data)) {
            // quit if insert fails - adjust accordingly
            print_r($data);
            die('Failed insert');
        }    
    }

    // don't redirect inside the loop
    redirect(base_url());

} else{
    echo 'No Data';
}
发布评论

评论列表(0)

  1. 暂无评论