First sorry, im a really big begginer, i dont really understand something.
I am building a small caht form my users, and the inserting is okay.
I would like to that when i insert my chat message, after that the inserted message would slide down, and im a bit stuck with it.
my jquery
$('#chat_submit').on('click', function(e){
e.preventDefault();
var res = {
'msg' : $('#chat_msg').val(),
}
$.ajax({
type: 'POST',
url: '<?php echo base_url(); ?>main/send_chat',
data: res,
dataType: 'json',
success: function(data, html){
$('#chat_msg').removeAttr('value');
}
});
return false;
});
html and php
<div id="content">
<h2>Chat</h2>
<div id="chatbox">
<?php foreach ($chat as $ch): ?>
<?php
echo '
<div class="chat_post">
<span class="chat_time">'.date('H:i', strtotime($ch->added)).'</span>
<a href="'.base_url().'profile/user/'.$ch->uid.'" class="chat_name">'.$ch->first_name. " " .$ch->last_name.': </a>
<span class="chat_msg">'.$ch->msg.'</span>
</div>
';
?>
<?php endforeach; ?>
</div>
<?php echo form_open(base_url() . "main/send_chat"); ?>
<div id="feed_wrapp">
<input type="text" name="msg" id="chat_msg">
<input type="submit" value="Küldés" class="submit" id="chat_submit">
</div>
<?php echo form_close(); ?>
</div>
i tried numerous ways but im totally clueless, could please someone point out what i am missing?
send chat function in my controller
function send_chat()
{
$this->load->model('user/chat_model');
if($this->input->is_ajax_request())
{
$this->chat_model->chat_insert();
}
//redirect(base_url());
}
and the insert in my model
function chat_insert()
{
$chat = array(
'uid' => $this->session->userdata('uid'),
'msg' => $this->input->post('msg')
);
$this->db->insert('pf_chat_post', $chat);
}
first modify your CI controller and model functions like this
and in your form add two hidden inputs named “first_name” and “last_name”
and then in your success function in $.ajax call add this :