0

I have an AJAX file upload code in codeigniter. The Issue is that I changed the simple form submit to file submit. But After that, JQUERY has stopped working. The response is coming success, but at the same time, ajax error function is called. I don't know what's wrong with my code.

This is my controller.

public function ajax_add() {
    $this->_validate();

    $config = [
    'upload_path' => './assets/game_images/',
    'allowed_types' => 'gif|png|jpg|jpeg'
    ];
    $this->load->library('upload', $config);
    if ($this->upload->do_upload('image')) {
        $file = $this->upload->data();
        $file_name = $file['file_name'];

        if ($file_name == '') {
            $data['error_string'][] = 'Please upload an image.';
            $data['status'] = FALSE;
            echo json_encode($data);
            exit();
        }
    } else {
        $data['inputerror'][] = 'image';
        $data['error_string'][] = $this->upload->display_errors();
        $data['status'] = FALSE;
        echo json_encode($data);
        exit();
    }

    $data = array(
        'title' => $this->input->post('title'),
        'iframe' => $this->input->post('iframe'),
        'status' => $this->input->post('status'),
        'category_id' => $this->input->post('category_id'),
        //'image' => $file_name
        );
    $insert = $this->game->save($data);
    echo json_encode(array("status" => TRUE));
}

private function _validate() {
    $data = array();
    $data['error_string'] = array();
    $data['inputerror'] = array();
    $data['status'] = TRUE;

    if ($this->input->post('title') == '') {
        $data['inputerror'][] = 'title';
        $data['error_string'][] = 'Game Title is required';
        $data['status'] = FALSE;
    }

    if ($this->input->post('iframe') == '') {
        $data['inputerror'][] = 'iframe';
        $data['error_string'][] = 'Game Iframe is required';
        $data['status'] = FALSE;
    }

    if ($this->input->post('status') == '') {
        $data['inputerror'][] = 'status';
        $data['error_string'][] = 'Status is required';
        $data['status'] = FALSE;
    }

    if ($this->input->post('category_id') == '') {
        $data['inputerror'][] = 'category_id';
        $data['error_string'][] = 'Please select category';
        $data['status'] = FALSE;
    }

    if ($data['status'] === FALSE) {
        echo json_encode($data);
        exit();
    }
}

And this is my HTML

if (save_method == 'add') {
            url = "<?php echo site_url('game/ajax_add') ?>";
        } else {
            url = "<?php echo site_url('game/ajax_update') ?>";
        }
var formData = new FormData($('#form')[0]);

        $.ajax({
            url: url,
            type: 'JSON',
            data: formData,
            async: false,
            success: function (data)
            {
                if (data.status) //if success close modal and reload ajax table
                {
                    $('#modal_form').modal('hide');
                    reload_table();
                } else
                {
                    for (var i = 0; i < data.inputerror.length; i++)
                    {
                        $('[name="' + data.inputerror[i] + '"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                        $('[name="' + data.inputerror[i] + '"]').next().text(data.error_string[i]); //select span help-block class set text error string
                    }
                }
                $('#btnSave').text('save'); //change button text
                $('#btnSave').attr('disabled', false); //set button enable 
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                alert('Error adding / update data');
                $('#btnSave').text('save'); //change button text
                $('#btnSave').attr('disabled', false); //set button enable 

            },
            cache: false,
            contentType: false,
            processData: false
        });
2
  • change ajax type to 'POST' Commented Oct 4, 2016 at 7:10
  • And add ajax dataType: 'JSON'. Commented Oct 4, 2016 at 7:46

1 Answer 1

2
$.ajax({
   type: 'POST',
   url: url,
   dataType: 'JSON',
   contentType: 'application/json; charset=utf-8'
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.