I’m working on a control panel page where users can change the state of their service. I have a CodeIgniter project where a function named activation sits to change the users state. It does all the database updates for state change.
HTML FILE
<html>
<head>
<script src="jquery.min.js"></script>
<script>
function activation (state)
{
$.ajax({
type: "POST",
dataType: "json",
data: {'status':state},
url: 'http://myservice.lan/cp/activation',
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/json;charset=UTF-8");
}
},
success: function(data)
{
var active = '<button onClick="activation(\'deactivate\')" >Deactivate</button>';
var inactive = '<button onClick="activation(\'activate\')" >Activate</button>';
if (data.ust == 'active')
{
$('#polo').html(active);
}
else(data.ust == 'inactive')
{
$('#polo').html(inactive);
}
}
});
}
</script>
</head>
<body>
<div id='polo'>
<button onClick="activation('activate')" >Activate</button>
</div>
</body>
</html>
PHP FUNCTION (A CodeIgniter Project)
I haven’t put the db related code into it yet.
public function activation()
{
$opt=$this->input->post('status', TRUE);
if ($opt=='activate') {
$data['ust']='active';
}
if ($opt=='deactivate') {
$data['ust']='inactive';
}
echo json_encode($data);
}
I am not able to get it working. The state changes once, then stays same even when repeatedly hitting the button.
I would like help regarding using better jQuery functionality to automatically pick up the state instead of passing it to the function.