I have a enable and disable button. Each buttons returns a "success" message in the console when clicked. The enable button works in a single click, however, the disable button needs to be clicked twice before it prints the "success" message in the console in my developer tools. What is the problem with this? Can somebody helps me fixing this one? Thanks a lot. Here is my code:
<button class="btn_mode" value="1">Enable</button>
<button class="btn_mode" value="0">Disable</button>
<script type="text/javascript">
$(".btn_mode").click(function(){
var mode_val = $(this).val();
var postdata={'mode':mode_val};
$.ajax({
type:"POST",
url:"<?php echo base_url();?>superadmin/change_mode",
dataType:'json',
data:postdata,
success: function(data){
if(data.notify=="Success")
{
console.log(data.notify);
location.reload();
}
else{
console.log(data.notify);
}
}
});
});
</script>
my controller function
function change_mode(){
$mode = $this->input->post('mode');
$query = $this->course_booking_model->change_mode($mode);
if($query){
$notification = "Success";
}
else{
$notification = "Failed";
}
echo json_encode(array('notify'=>$notification));
}
model
function change_mode($mode){
$id = 1;
$data = array('mode' => $mode);
$this->db->where('id',$id);
$this->db->update('maintenance_mode',$data);
return true;
}
Output when adding the console.log('Posting mode_val: ' + mode_val);
in the javascript
Clicking the enable button output in the console
Output in the console when clicking the disable button