Code igniter get ajax value from view to controller

后端 未结 4 1781
忘了有多久
忘了有多久 2021-01-07 06:39

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

Ajax

$.ajax({
    type: \"POST\",
    url: \"e         


        
相关标签:
4条回答
  • 2021-01-07 07:08

    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'].

    0 讨论(0)
  • 2021-01-07 07:27

    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;
                }  
           }
    
    0 讨论(0)
  • 2021-01-07 07:27

    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');
    
    0 讨论(0)
  • 2021-01-07 07:28

    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'];
      }
    }
    
    0 讨论(0)
提交回复
热议问题