简体   繁体   English

如何在单击按钮时将值从 ajax 传递给控制器​​?

[英]how to pass values from ajax to controller on button click?

this is my js code which is executed on button click这是我的 js 代码,它在按钮单击时执行

    $("#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 .在此处阅读有关load()函数的信息 It sends a new empty request and load that data to your element.它发送一个新的空请求并将该数据加载到您的元素。 It doesn't load the response.它不加载响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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