我有一个 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();
}