我有一个Codeingiter应用程序,使用jquery将帖子存储到数据库中!但问题是它存储了两次相同的数据!我查看了代码,但我似乎无法弄明白!请帮我! 视图:
<div id="poster">
<label id="poster_label" > State your feeling.. </label> <br />
<textarea rows="3" cols="60" id="poster_textarea" ></textarea> <br />
<input type="button" id="poster_button" value="Publish" />
</div>
JS:
$('#poster_button').click(function(){
if(!$('#poster_textarea').val()){
$('#poster_textarea').css('border','1px solid pink').focus();
return false;
}else{
var poster_textarea = $('#poster_textarea').val();
var url = "<?php echo base_url();?>index.php/blog/add_post";
$.ajax({
type: "POST",
url: url,
data: 'poster_textarea='+poster_textarea,
//datatype: "json",
success: function()
{
alert('saved');
}
});
}
});
控制器:
function add_post(){
$post_data = array(
'id' => '',
'user_id' => '11330',
'content' => $this->input->post('poster_textarea'),
'date_time' => date("Y-m-d H:i:s"),
'status' => '1'
);
$this->blog_model->add_post($post_data);
}
型号:
function add_post($post_data){
$this->db->trans_start();
$this->db->insert('posts',$post_data);
$this->db->trans_complete();
}
答案 0 :(得分:1)
在你的JS中尝试这个。 就我想的那样,它创建了DOM树。
$('#poster_button').click(function(e){
e.stopImmediatePropagation();
if(!$('#poster_textarea').val()){
$('#poster_textarea').css('border','1px solid pink').focus();
return false;
}else{
var poster_textarea = $('#poster_textarea').val();
var url = "<?php echo base_url();?>index.php/blog/add_post";
$.ajax({
type: "POST",
url: url,
data: 'poster_textarea='+poster_textarea,
//datatype: "json",
success: function()
{
alert('saved');
}
});
}
});