[eluser]shinokada[/eluser]
The following codes works without jquery.
Could anyone have a look at it and point out what I am doing wrong please?
Thanks in advance.
I have the following controller.
controllers/admin/messages.php
Code:
function getShoutBox(){
$data['title'] = "getshoutbox";
$data['main'] = 'admin_home';
$data['messages']=$this->MMessages->getMessages();
$this->load->vars($data);
$this->load->view('dashboard');
}
function insertShoutBox(){
$data['title'] = "insertshoutbox";
$data['main'] = 'admin_home';
$this->MMessages->updateMessage();
$data['messages']=$this->MMessages->getMessages();
$this->load->vars($data);
$this->load->view('dashboard');
}
View is this one.
Code:
<form method="post" id="form" action="index.php/admin/messages/insertShoutBox" >
<label>User</label>
<input class="text user" name="user" id="nick" type="text" MAXLENGTH="25" />
<label>Message</label>
<input class="text" id="message" name="message" type="text" MAXLENGTH="255" />
<input id="send" type="submit" value="Shout it!" />
</form>
<div id="container">
<ul class="menu">
<li>Shoutbox</li>
</ul>
<span class="clear"></span>
<div class="content">
<h1>Latest Messages</h1>
<div id="loading"><img src="images/general/loading.gif" alt="Loading..." /></div>
<?php
foreach ($messages as $key => $list){
print_r ($list);
}
?>
</div>
</div>
<?php
print_r ($_POST['user']);
?>
And jquery is this one.
Code:
$(document).ready(function(){
//global vars
var inputUser = $("#nick");
var inputMessage = $("#message");
var loading = $("#loading");
var messageList = $(".content > ul");
//functions
function updateShoutbox(){
//just for the fade effect
messageList.hide();
loading.fadeIn();
//send the post to shoutbox.php
$.ajax({
type: "POST",
url: "admin/messages/getShoutBox",
// data: "action=update",
complete: function(data){
loading.fadeOut();
messageList.html(data.responseText);
messageList.fadeIn(2000);
}
});
}
//check if all fields are filled
function checkForm(){
if(inputUser.attr("value") && inputMessage.attr("value"))
return true;
else
return false;
}
//Load for the first time the shoutbox data
updateShoutbox();
//on submit event
$("#form").submit(function(){
if(checkForm()){
var nick = inputUser.attr("value");
var message = inputMessage.attr("value");
//we deactivate submit button while sending
$("#send").attr({ disabled:true, value:"Sending..." });
$("#send").blur();
//send the post to shoutbox.php
$.ajax({
type: "POST",
url: "admin/messages/insertShoutBox",
data: $('#form').serialize(),
complete: function(data){
messageList.html(data.responseText);
updateShoutbox();
//reactivate the send button
$("#send").attr({ disabled:false, value:"Shout it!" });
}
});
}
else alert("Please fill all fields!");
//we prevent the refresh of the page after submitting the form
return false;
});
});
models/mmessages.php
Code:
...
function updateMessage(){
$data = array(
'message' => db_clean($_POST['message']),
'user' => db_clean($_POST['user'])
);
$this->db->insert('shoutbox', $data);
}
function getMessages(){
// $this->db->limit(10);
$this->db->order_by("date", "desc");
$Q = $this->db->get('shoutbox');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}