-1

I am new in CI and I am trying to upload image but dont know why its not uploading, Here is my view,

<form action="<?php echo site_url('admin/test/'.$param2.'/add'); ?>" enctype="multipart/form-data" method="post" id = 'mcq_form'>
    <input type="hidden" name="question_type" value="mcq">
    <div class="input-group">
        <div class="custom-file">
            <input type="file" class="custom-file-input" id="attachment" name="attachment" onchange="changeTitleOfImageUploader(this)">
            <label class="custom-file-label" for="attachment"><?php echo get_phrase('attachment'); ?></label>
        </div>
    </div>
    <div class="text-center">
        <button class = "btn btn-success" id = "submitButton" type="button" name="button" data-dismiss="modal"><?php echo get_phrase('submit'); ?></button>
    </div>
</form>

<script>
    $('#submitButton').click( function(event) {
        $.ajax({
            url: '<?php echo site_url('admin/test/'.$param2.'/add'); ?>',
            type: 'post',
            data: $('form#mcq_form').serialize(),
            success: function(response) {
               if (response == 1) {
                   success_notify('<?php echo get_phrase('question_has_been_added'); ?>');
               }else {
                   error_notify('<?php echo get_phrase('no_options_can_be_blank_and_there_has_to_be_atleast_one_answer'); ?>');
               }
             }
        });
        showLargeModal('<?php echo site_url('modal/popup/test/'.$param2); ?>', '<?php echo get_phrase('test'); ?>');
    });
</script>

on Controller I am trying to get image data but dont know why its not fetching,

print_r($_FILES['attachment']['name']);
die();

I don't understand, what I am missing. Please help me out.

Bhawesh
  • 47
  • 9
  • you are using form action and ajax at the same time to submit form data remove action from the form or remove ajax request use once at a time – Robin Hood May 26 '22 at 09:50
  • and first print_r($_FILES); to check if image data received or not – Robin Hood May 26 '22 at 09:50
  • Does this answer your question? [jQuery Ajax File Upload](https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload) – Don't Panic May 29 '22 at 14:54
  • https://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery, https://stackoverflow.com/questions/44708023/how-to-upload-a-file-using-ajax-on-post, https://stackoverflow.com/questions/10899384/uploading-both-data-and-files-in-one-form-using-ajax, https://stackoverflow.com/questions/46648570/php-ajax-file-upload-is-not-working, ... – Don't Panic May 29 '22 at 14:56

1 Answers1

0

You can try it like this

  <input type="file" name="logo" class="form-control" value="">

in your controller

$logo = '';
if (!empty($_FILES['logo']['name'])) {
    /* Conf Image */
    $file_name = 'profile_' . time() . rand(100, 999);
    $configImg['upload_path'] = './uploads/profile/';
    $configImg['file_name'] = $file_name;
    $configImg['allowed_types'] = 'png|jpg|jpeg';
    $configImg['max_size'] = 2000;
    $configImg['max_width'] = 2000;
    $configImg['max_height'] = 2000;
    $configImg['file_ext_tolower'] = TRUE;
    $configImg['remove_spaces'] = TRUE;

    $this->load->library('upload', $configImg, 'logo');
    if ($this->logo->do_upload('logo')) {
        $uploadData = $this->logo->data();
        $logo = 'uploads/profile/' . $uploadData['file_name'];
    } else {
        $this->custom_errors['logo'] = $this->logo->display_errors('', '');
    }
}