I need to pass a variable value from javascript to controller. The value is obtained by select option in html, and the javascript code is inside the view. I've written the code but it's just showing the value that I choose to HTML, and it's not stored in php variable.
Here's my javascript code :
$('#button-search-ohr').on('click', function() {
var month = $('#monthChoose').val();
if(month == 0) {
alert('Month must be choosen!');
}
$.ajax({
async : true,
type : 'POST',
url : '<?php echo base_url();?>sms/lapkinerjastat', //this url to controller
data : {month:month},
success : function(data) {
$('#month').html(data);
}
});
}
And here's my html :
<h5 class="pilih-bulan">Month Choose
<select id="monthChoose" name="month">
<option value="0">Pilih bulan</option>
<option value="Januari">Januari</option>
<option value="Februari">Februari</option>
<option value="Maret">Maret</option>
<option value="April">April</option>
<option value="Mei">Mei</option>
<option value="Juni">Juni</option>
<option value="Juli">Juli</option>
<option value="Agustus">Agustus</option>
<option value="September">September</option>
<option value="Oktober">Oktober</option>
<option value="November">November</option>
<option value="Desember">Desember</option>
</select>
<button id="button-search-ohr" type="submit">Search</button>
</h5>
<div id='month'>
<?php
if(isset($_POST['month'])) {
$month = $_POST['month'];
echo 'This is '.$month;
}else {
$month = null;
echo 'Nothing';
}?>
</div>
Note : It'll be better with ajax to pass variable to controller.
select option. I'd like to pass the variable in javascript and store it to controller in php.