简体   繁体   中英

how to pass values from ajax to controller on button click?

this is my js code which is executed on button click

    $("#search_tutor").click(function(){
    $("#show_tutors").hide();
    var subject = $( ".subject" ).val();
    var degree_level = $(".degree_level").val();
    var city = $(".city").val();
    console.log(subject);
    console.log(degree_level);
    console.log(city);
      $.ajax({
      url:"<?php echo base_url(); ?>public_controller/search_tutor_jquery",
      method:"post",
      data:{subject: subject, degree_level : degree_level, city : city},
      success:function(data){
        $('#search_results').html(data);

      }
    });

    });

and this is controller function

        public function search_tutor_jquery(){
  $subject = strtolower($this->input->post('subject'));
  $degree_level = strtolower($this->input->post('degree_level'));
  $city = strtolower($this->input->post('city'));
  echo $subject; //prints nothing
  $data['filtered_tutors'] = $this->public_model->search($subject,$degree_level,$city);
  $this->load->view('public/search_results',$data);
}

values are being logged in console but i cannot receive values in controller. can somebody help me out? thanks in advance

Have you tried using

$_POST['subject']

instead of

$this->input->post('subject')

?

What do you mean by

$('#search_results').load("<?php echo base_url('public_controller/search_tutor_jquery') ?>");

Try

$('#search_results').html(data);

Read about load() function here . It sends a new empty request and load that data to your element. It doesn't load the response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM