1

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.

4

3 回答 3

4

您的 else 语句具有不会影响您应该使用的行为的语句else if

你可以自己看看这里

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 if(data.ust == 'inactive') // instead of else(data.ust == 'inactive')
    {
        $('#polo').html(inactive);
    }
}
于 2013-07-24T11:35:18.167 回答
2

您正在分配值,而不是进行比较。

尝试

if ($opt=='activate') {
    $data['ust']='active';
} 
if ($opt=='deactivate') {
    $data['ust']='inactive';
}
于 2013-07-24T11:24:10.443 回答
0

It should be == instead of = while checking if conditions.

$opt='activate'
于 2013-07-24T11:25:57.973 回答