Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Ajax POST in Codeigniter

I have searched a lot of tutorials with POST methods and saw answered questions here too but my POST still doesn't work...I thought i should post it here if you guys see something that i don't!

My js - messages.js:

$(document).ready(function(){   

    $("#send").click(function()
    {       
     $.ajax({
         type: "POST",
         url: base_url + "chat/post_action", 
         data: {textbox: $("#textbox").val()},
         dataType: "text",  
         cache:false,
         success: 
              function(data){
                alert(data);  //as a debugging message.
              }

     return false;
 });
 });

My view - chat.php:

<?php $this->load->js(base_url().'themes/chat/js/messages.js');?> //i use mainframe framework which loading script this way is valid



<form method="post">
    <input id="textbox" type="text" name="textbox">
    <input id="send" type="submit" name="send" value="Send">
</form>

Last My controller - chat.php

//more functions here

function post_action()
{   
    if($_POST['textbox'] == "")
    {
        $message = "You can't send empty text";
    }
    else
    {
        $message = $_POST['textbox'];
    }
    echo $message;
}
like image 460
Dennis Avatar asked Jul 11 '13 10:07

Dennis


1 Answers

$(document).ready(function(){   

    $("#send").click(function()
    {       
     $.ajax({
         type: "POST",
         url: base_url + "chat/post_action", 
         data: {textbox: $("#textbox").val()},
         dataType: "text",  
         cache:false,
         success: 
              function(data){
                alert(data);  //as a debugging message.
              }
          });// you have missed this bracket
     return false;
 });
 });
like image 56
Ganesh RJ Avatar answered Oct 13 '22 18:10

Ganesh RJ