CodeIgniter - How do I pass a value from a view to a controller?

I want to pass the value that I take from javascript to the controller. But I do not use any function. I just want to access the value that I get from the view in the controller. I have the following view.

<select id="selected_year" name="selected_year" data-live-search="true" style="margin-left: 27px;" > <option value="2015">2015</option> <option value="2016">2016</option> <option value="2017">2017</option> <option value="2018">2018</option> <option value="2019">2019</option> <option value="2020">2020</option> <option value="2021">2021</option> <option value="2022">2022</option> <option value="2023">2023</option> <option value="2024">2024</option> </select> 

I get the value of the dropdown using the following javascript.

 <script type="text/javascript"> $("#selected_year").live('change', function() { var selected_year = $(this).attr("value"); alert(selected_year); window.location.reload(); }); </script> 

I want to just pass the value that I get from the above javascript to the controller. Can this be done?

+5
source share
4 answers

You would do this by sending a POST request that will be sent to the controller action (synchronously or asynchronously, depending on your needs) or passing it as a query string in the URL, and then checking for its presence in your controller.

+1
source

You can send a data form view to a controller in three ways.

 1.POST ,GET(form submitio). 2.making it in link(URi segment) 3.via Ajax 
0
source

You can use AJAX to submit a drop-down list of changes every time

 var data = $('#selected_year').val(); $("selected_year").on('change',function(){ $.ajax({ url: yourcontroller, type: 'POST', data: data, success: function (data) { }, }); }); 
0
source

Create a controller to provide the dynamic portion of the URL.

 <script type="text/javascript"> $("#selected_year").live('change', function() { var selected_year = $(this).attr("value"); // NEXT 2 LINES ARE THE SOLUTION. substitute your own controller URL on the next line. $url = '<?= base_url()index.php/controller/method?>/'+selected_year+''; window.location.reload($url); }); </script> 

Here is your controller:

 function method($year){ echo $year; } 
-1
source

All Articles