Jquery,codeigniter 2.1文件上传问题


Jquery, codeigniter 2.1 issue with file upload

我有这样的jquery代码:

$(parent).on('click', '.izmeni', function() {
        var clone = $(this).parent().clone();
        var name = $(this).siblings('.name').text();
        var surname = $(this).siblings('.surname').text();
        var info = $(this).siblings('.info').text();
        var email = $(this).siblings('.email').text();   
        var phone = $(this).siblings('.phone').text();   
        var date_birth = $(this).siblings('.date_birth').text();   
        var id_lok = $(this).siblings('.lokacija').val();    
        var id = $(this).siblings('.id').val();             
        <?php $attributes = array('class' => 'update_form'); ?>
        $(this).hide();
        $(this).siblings('.permission').hide();
        $(this).siblings('strong').hide();
        $(this).parent().wrapInner('<?php echo form_open_multipart('upload/do_upload', $attributes);?></form>');
        $(this).parent().prepend('<input type="file" name="userfile" size="20" />');
        $(this).parent().append('<div class="lokacija"></div>');
        $(this).parent().append('<select name="pol"><option value="m">Muški</option><option value="f">Ženski</option></select>');        
        $(this).parent().append('<input type="submit" value="Submit" />');
        $(this).parent().append('<a id="otkazi">Otkazi</a>');
        $(this).siblings('.name').replaceWith('<input name="name" value="' + name + '" />');
        $(this).siblings('.surname').replaceWith('<input name="surname" value="' + surname + '" />');
        $(this).siblings('.info').replaceWith('<textarea name="info">' + info + '</textarea>');       
        $(this).siblings('.email').replaceWith('<input name="email" value="' + email + '" />');
        $(this).siblings('.phone').replaceWith('<input name="phone" value="' + phone + '" />');          
        $(this).siblings('.date_birth').load("<?php echo base_url() ?>form/date_birth", {'date' : date_birth});   
        $(this).siblings('.lokacija').load("<?php echo base_url() ?>form/location", {'id' : id_lok});   
        $(this).siblings('#otkazi').click(function(){
            $(this).parents("div:first").replaceWith(clone);
            $(this).show();
        });
        $(".update_form").submit(function(){
            data = $(this).serialize();
            console.log(data);
            forma = $(this);
            $.ajax({
                type : "POST",
                data : data,
                url : "<?php echo base_url() ?>/user/update_person",
                success : function(){
                    forma.slideUp('slow');
                    $("#all_users").fadeOut('slow', function(){
                        $(this).empty().load("<?php echo base_url() . "ajax/get_all_users" ?>", function(){
                            $(this).hide().slideDown('slow');
                        })
                    });
                }
            })
            return false;
        });
    });

和PHP代码:

function update_person(){
           if($this->person_img_upload() != false){$data['picture'] = $this->person_img_upload();}else{echo "131231";}
               $data = array(
            'name' => $_POST['name'],
            'surname' => $_POST['surname'],
            'telephone' => $_POST['phone'],
            'info' => $_POST['info'],            
            'location' => $_POST['location'],
            'gender' => $_POST['pol'],
            'date_birth' => $this->date()
               );
            $id = $_POST['id'];
            echo $id;
            $this->db->where('id_person', $id);
            $this->db->update('person', $data);
            $this->update_user($id);
           }

function person_img_upload(){
               $config = array (
            'allowed_types' => 'jpg|jpeg|png|gif',
            'upload_path' => $this->path
        );
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $q = $this->upload->data();
        if ( !$this->upload->do_upload()){
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_form', $error);
            return false;}
        else{
            $this->upload->do_upload(); 
            $q = $this->upload->data();        
            return $q['file_name'];
                }     
           }

Jquery正在替换页面上的元素,它创建了一个表单,用户可以在其中更新数据。(这很好,没有图像部分的更新也很好)。当我尝试更新图像时,我得到了这个错误:

您没有选择要上传的文件

我还有创建新用户的表单,使用

函数person_img_upload()

它运行良好(表单不是通过jquery创建的)。有什么问题吗?

文件不是通过ajax请求上传的。你需要提交你的整个表单,或者如果你想让它通过ajax完成,那么你可以通过iframe发布你的表单。