Welcome Guest, Not a member yet? Register   Sign In
My Ajax/jquery code is not working
#1

[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>
&lt;input class="text user" name="user" id="nick" type="text" MAXLENGTH="25" /&gt;
<label>Message</label>
&lt;input class="text" id="message" name="message" type="text" MAXLENGTH="255" /&gt;
&lt;input id="send" type="submit" value="Shout it!" /&gt;
    
&lt;/form&gt;
<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>
    
    &lt;?php
    foreach ($messages as $key => $list){
        
    print_r ($list);
    }
    ?&gt;
        
</div>
</div>
    
&lt;?php
print_r ($_POST['user']);
?&gt;

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;
    
}
#2

[eluser]mcr_rm[/eluser]
Check your post to urls....

they are admin/messages/insertShoutBox

when they probably need to be absolute as jQuery won't understand the codeigniter pathing in the same way the forms do....

so /index.php/admin/messages etc etc.

Give it whirl first anyways. You using firebug on firefox? anyway of see the javascript jQuery error to confirm, I would guess at the moment it is posting to the wrong place.
#3

[eluser]shinokada[/eluser]
Thanks. You are quite right.
I added index.php and insert data.

However it does not show the updated message.

Any help will be appreciated.

Thanks in advance.




Theme © iAndrew 2016 - Forum software by © MyBB