Passing data from Codeigniter controller to div using ajax

I am trying to make a simple script where it is made by an ajax call to a controller and its result is printed inside a div.

AJAX

 $('#search-bar-button').click(function(){
       var search_data=$('#search-bar').val();         
       $.ajax({
            url: "<?php echo base_url('search/ajax') ?>",
            type: 'POST',
            data: "data="+search_data,
            dataType: "html",
            success: function(msg) {                    
                $('#result').html(msg);
            }
       });             
       event.preventDefault();             
       //return false;          
  });

      

AJAX CONTROLLER FUNCTION / application / controller / search.php

 public function ajax()
     {
    //$this->load->library('filtri');

    $data=$this->input->post('search_data');
    echo "<p>result= ".$data."</p>";
     }

      

VIEW

   <div class="span7">
 <?php echo form_open('validation/ricerca'); ?>
      <?php echo form_input(array('id'=>'search-bar');?>
     <?php echo form_submit(array('value'=>'Search','id'=>'search-bar-button'));?>
  <?php echo form_close(); ?>
   </div>
  <div class="well" id="result"></div>

      

+3


source to share


2 answers


Here's What You Can Do



public function ajax()
{
    //$this->load->library('filtri');
    $data   =   $this->input->post('data');
    echo "<p>result= ".$data."</p>";
}

      

0


source


Please check your data variable. Instead of "data", you read "search_data". In your controller, the ajax function uses $ this-> input-> post ('data').



0


source







All Articles