Welcome Guest, Not a member yet? Register   Sign In
Ajax to display multiple error messages.

I have a multiple upload function using json, on my view each file that has a error should display the file upload error message.

I can see that if I upload 3 images and all 3 images have errors only 2 of the messages will display on the view.

I use ajax to submit my form.

How can I make sure that if all 3 images would display error messages for all 3 rather than just 2 on view.

Ajax Script

<script type="text/javascript">
$('#button-upload').on('click', function() {

    $('body').prepend('<form enctype="multipart/form-data" id="upload-form" style="display: none;"><input type="file" name="userfile[]" value="" multiple="multiple" /></form>');

    $('#upload-form input[name=\'userfile[]\']').trigger('click');

    if (typeof timer != 'undefined') {

    timer = setInterval(function() {
        if ($('#upload-form input[name=\'userfile[]\']').val() != '') {

                url: 'index.php?c=filemanager&m=upload<?php echo $directory;?>',
                type: 'post',
                dataType: 'json',
                data: new FormData($('#upload-form')[0]),
                cache: false,
                contentType: false,
                processData: false,
                success: function(json) {
                    if (json['error']) {
                        $('#messages').append('<div class="alert alert-danger">' + json['error'] + '</div>');

                    if (json['success']) {
                        $('#messages').append('<div class="alert alert-success">' + json['success'] + '</div>');
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    }, 1000);


PHP Code:
public function upload() {

    $json = array();

    if (!$json) {

        $files $_FILES;

        if ($files) {

            $check_if_any_error 0;

            $fieldname 'userfile';

            $cpt count($_FILES[$fieldname]['name']);
($i=0$i<$cpt$i++) {           
[$fieldname]['name']= $files[$fieldname]['name'][$i];
                $_FILES[$fieldname]['type']= $files[$fieldname]['type'][$i];
                $_FILES[$fieldname]['tmp_name']= $files[$fieldname]['tmp_name'][$i];
                $_FILES[$fieldname]['error']= $files[$fieldname]['error'][$i];
                $_FILES[$fieldname]['size']= $files[$fieldname]['size'][$i];  


                $directory $this->input->get('image_directory');

                if (isset($directory)) {
                    $url .= $directory '/';
                } else {
                    $url .= '';

['upload_path'] = FCPATH 'images/catalog/' $url;
                $config['allowed_types'] = 'gif|png';
                $config['max_size'] = 10000;
                $config['max_width'] = 0;
                $config['max_height'] = 0;
                $config['overwrite'] = TRUE;


                if (!$this->upload->do_upload($fieldname)) {



            if ($check_if_any_error ) {

                $json['error'] = $this->upload->display_errors();

            } else {

                $json['success'] = 'You have upload your selected files!';





There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!

You don't need this part at all:

PHP Code:
if (!$json) { 

since $json will always be not true.

Also, you want to save error for every not uploaded file so correct nesting would be:

PHP Code:
if ( ! $this->upload->do_upload($fieldname))
   $json['error'][$i] = $this->upload->display_errors();//note that $json['error'] becomes array

After that what is left is to save success message if all files are uploaded with no errors:

PHP Code:
if ((int)$check_if_any_error 1)//or alternatively if (count($json['error']) < 1)
   $json['success'] = 'You have upload your selected files!';

Then, handle response well as you get (if any) array for errors.

Theme © iAndrew 2016 - Forum software by © MyBB