2

How do I get send in my controller? This is what, I have tried out:

Ajax

$.ajax({
    type: "POST",
    url: "example/name",
    data: send,
    success: function(value) {

    }
});

Controller

class Example extends CI_Controller {
    function name() {
        $this - > post(send);
    }
}
dfsq
  • 191,768
  • 25
  • 236
  • 258
ram
  • 1,111
  • 6
  • 15
  • 24
  • 1
    What is `send` variable? – dfsq Mar 23 '13 at 08:23
  • @dfsq:it has an string as content. How do I send ajax values and retrieve in controller. – ram Mar 23 '13 at 08:25
  • Are you sure that there is a function named `post` exists on your controller ? I hope that you need to fetch the `POST` values, Please use `$this->input->post('field_name')` – Red Mar 23 '13 at 09:31

4 Answers4

2

Seems that you don't send your data properly. Try this:

$.ajax({
    type: "POST",
    url: "example/name",
    data: {send: send},
    success: function(value) {

    }
});

In this case you will be available as $_POST['send'].

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • How do I retrieve in my controller ? – ram Mar 23 '13 at 08:26
  • You can get it as any other POST parameter. I'm not familiar with Codeigniter, but after quick lookup in documentation seems that you can retrieve it as `$this->input->post('send');` as well. – dfsq Mar 23 '13 at 08:27
  • :In my firebug, it is showing me example/index/example/name. Why is it posting like this ? my code igniter says An Error Was Encountered The action you have requested is not allowed. – ram Mar 23 '13 at 08:39
  • Because the path must be `/example/name`. Front `/` makes it to be relative to the web server root. – dfsq Mar 23 '13 at 09:55
1

Try this one , this is ajax call

 $.post('<?php echo base_url()?>example/name',{send:send}, 
          function(data) {

      });

then access it using post into your controller like this

class Example extends CI_Controller {
function name() {
    $_POST['send'];
  }
}
Azam Alvi
  • 6,918
  • 8
  • 62
  • 89
  • :Now my url structure is correct but mu fire bug shows POST http://localhost/sample/index.php/example/name 500 Internal Server Error 195ms. When I land up in this page my URL is look like this http://localhost/sample/index.php/example/data/13 – ram Mar 23 '13 at 09:04
1

First you can define a global variable that can be used as your base url in the jquery code. Place this in the <script> tag of the page <head> section

         //<![CDATA[
              base_url = '<?php echo base_url();?>';
        //]]>

Than do the ajax request like this

        var data  = 'var1=aaa&var2=bbb';

        $.ajax({
            type: "POST",
            url: base_url+"mainController/getData/", //base_url is the variable which you have defined in the head section 
            data: data,
            success: function(response){
                   alert(response);
            }
        });

Than in the controller retrieve the post data like this

       class MainController extends CI_Controller {

            function getData()
            {
               $var1 = $this->input->post('var1');
               $var2 = $this->input->post('var2');

               echo $var1;
               echo '<br/>';
               echo $var2;
            }  
       }
Khawer Zeshan
  • 9,470
  • 6
  • 40
  • 63
0

You should specify the value to be posted with the send i.e. it should be like-

$.ajax({
    type: "POST",
    url: "example/name",
    data: 'send='+1,
    success: function(value) {

    }
});

Then you will have the value of this variable as you are doing.

using-

$this->input->post('send');
Prateek Shukla
  • 593
  • 2
  • 7
  • 27