cannot upload file using jconfirm and ajax in codeigniter

37 Views Asked by At

My upload process without jconfirm works well, but when I need to use jconfirm library to upload the file, it always says undefined index: myfile.

Here is my code

 function upload() {
        // body...
        $.confirm({
          title:'Upload Dental Form',
          type:'green',
          theme:'material',
          content: '<form method="post" id="myform" enctype="multipart/form-data">'    
                              +'<label for="file" id="up"><h3>Select Dental Form</h3></label> <br><br>'
                              +'<input accept="image/*" type="file" name="myfile" id="myfile" '
                              +' /> ' 
                 +'</form>',
          buttons: {
            save: {
              text:'save',
              btnClas: 'btn-blue',
              action:function() {
                $.ajax({  
                     url:"<?php echo base_url(); ?>Dental/form_upload",   
                     //base_url() = http://localhost/tutorial/codeigniter  
                     method:"POST",  
                     data: $('form').serialize(),  
                     contentType: false,  
                     cache: false,  
                     processData:false,  
                     success:function(data)  
                     {  
                      alert(data);
                     }  
                });  
              }
            },
            cancel:function() {

            }
          },
          onContentReady: function () {
        // bind to events
        var jc = this;
        this.$content.find('form').on('submit', function (e) {
            // if the user submits the form by pressing enter in the field.
            e.preventDefault();
            jc.$$save.trigger('click'); // reference the button and click it
        });
    }
        });
      }

in my controller

 function form_upload()  
      {  
            echo json_encode(print_r($_FILES["myfile"]["name"]));
      } 

I'm just echoing for debugging purposes.

The response was undefined index: myfile. Why do I get this error? I have tried using new formData(this) instead of $('form').serialize() but it doesn't work either.

0

There are 0 best solutions below