admin管理员组文章数量:1023803
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>» 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>» 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 badges1 Answer
Reset to default 4There 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';
}
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>» 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>» 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 badges1 Answer
Reset to default 4There 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';
}
本文标签: javascriptCodeIgniter dynamic input with jquery and insert to databaseStack Overflow
版权声明:本文标题:javascript - CodeIgniter dynamic input with jquery and insert to database - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745555594a2155856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论