Bill Garrison
Bill Garrison

Reputation: 2227

Codeigniter 2 - inserting into table creates duplicates

So I created a simple tally table that contains three columns 'id' , 'type', 'value'. When someone clicks on a button it creates a new row in this table.

javascript that calls my action:

        $('#myModal .ux-options .btn').on('click', function(){
            var value = $(this).data('value'); 
            // Update the session variable via ajax
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: 'applications/tallyAnswer',
                cache: false,
                data: {selection: value}
            });

            $('#myModal').modal('hide');
        });

controller action:

    function tallyAnswer() {
        $selection = $this->input->post('selection');
        if ($selection) {
            $this->tally->updateTally(array(
               'type' => 'ux-select',
               'value' => $selection
            ));
            $this->session->set_userdata(array(
                'answeredQuestion' => 'true'
            ));
            $response = array(
                'status' => 'success',
                'selection' => $selection
            );
        } else {
            $response = array(
                'status' => 'fail',
                'data' => array(
                    'selection' => 'Must choose a selection'
                )
            );
        }
        $this->output->set_content_type('application/json');
        $this->output->set_output(json_encode($response));
    }

Model method:

public function updateTally($tally) {
    $this->db->insert('tallies', $tally);
}

So a person clicks a button and it inserts a new row into the table. Simple. However, what happens is that the person clicks the button and it inserts two identical rows. I have checked and only one ajax call is made. I am not sure why this is happening. Im being forced into using old codeigniter code so i think it may be an issue with using 2.2.

Upvotes: 1

Views: 74

Answers (1)

Bill Garrison
Bill Garrison

Reputation: 2227

So it seems that this is in fact an old issue with codeigniter 2: https://github.com/bcit-ci/CodeIgniter/issues/2473

In older versions if you try to insert data with the word "select" in it then you would get multiple inserts. I changed my "type" to "ux-choice" instead and it fixed my issue. Hope this helps anyone that has this issue in the future!!

Upvotes: 1

Related Questions