即使我上传图片,上传图片错误也无法隐藏

时间:2018-05-02 13:40:47

标签: php html codeigniter

我在codeigniter的后端进行表单验证,在客户端上每个字段都可以正常工作但在图像字段上错误仍然存​​在,即使我附加图像...下面是我的代码,我附加图像screenshort以获取更多细节。请在这方面提供帮助。enter image description here

这是我的控制器代码

  public function save(){
    $res = array();
    //form field validation rules
    $this->form_validation->set_rules('titlemaster', 'Main Title', 'required');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => 'Ourpeople Main Title Required'));
        exit;
    }   
    $this->form_validation->set_rules('detailmaster', 'Main Detail', 'required');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => 'Oupeople Main Description Required'));
        exit;
    }   
    $config['upload_path'] = 'fassets/images/oupeople';
    $config['allowed_types'] = 'jpg|jpeg|png|gif';
    $config['file_name'] = $_FILES['userfile']['name'];     
    $config['overwrite'] = TRUE;
    //Load upload library and initialize configuration
    $this->load->library('upload', $config);

    $ourpeoplesection = ($this->input->post('name')) . ($this->input->post('desig')) . ($this->input->post('aboutme')) . ($this->input->post('certification')) . ($this->upload->do_upload('userfile'));

    if (!empty($ourpeoplesection))
    {   
        $this->form_validation->set_rules('name', 'Name', 'required|max_length[25]');
        if (!$this->form_validation->run()) {
            echo json_encode(array('mes' => 'text-danger', 'msg' => 'Name Required'));
            exit;
        }           
        $this->form_validation->set_rules('desig', 'Designation', 'required|max_length[20]');
        if (!$this->form_validation->run()) {
            echo json_encode(array('mes' => 'text-danger', 'msg' => 'Designation Required'));
            exit;
        }   
        $this->form_validation->set_rules('aboutme', 'About Employee', 'required'); 
        if (!$this->form_validation->run()) {
            echo json_encode(array('mes' => 'text-danger', 'msg' => 'About Employee Required'));
            exit;
        }       
        $this->form_validation->set_rules('certification', 'Certification', 'required');
        if (!$this->form_validation->run()) {
            echo json_encode(array('mes' => 'text-danger', 'msg' => 'Certification Required'));
            exit;
        }           

        if(!$this->upload->do_upload('userfile'))
        {
            $error = 'Please upload Image file. <br>';
            //$error = $this->upload->display_errors('', '<br>');
            echo json_encode(array('mes' => 'alert-danger', 'msg' => $error));
            exit;           
        }

        $userData = array (
            'name' => $this->input->post('name'),
            'desig' => $this->input->post('desig'),
            'aboutme' => $this->input->post('aboutme'),
            'certification' => $this->input->post('certification'),
            'facebook' => $this->input->post('facebook'),
            'twitter' => $this->input->post('twitter'),
            'instagram' => $this->input->post('instagram'),
            'google' => $this->input->post('google'),           
            'userfile' =>  $this->upload->data('file_name'),
        );

        //Pass user data to model
        $insertData = $this->Ourpeoplemodel->insert($userData);         
    }
    $ourpeopleMaster = array(
        'titlemaster' => $this->input->post('titlemaster') ,
        'detailmaster' => $this->input->post('detailmaster')  
    );
    $insertUserData = $this->Ourpeoplemodel->insert_main($ourpeopleMaster);  
        //Storing insertion status message.
    if(($insertUserData && $insertData)){
        $res = array(
            'mes' => 'text-success',
            'msg' => "Record has been saved successfully.",
        );
        echo json_encode($res);
    } else {
        $res = array(
            'mes' => 'text-danger',
            'msg' => "Record not saved",
        );
        echo json_encode($res);
    }      
}   

这里是我的html输入

<div class="form-group row">                                    
                <div class="col-md-6 col-xs-12">
                    <?php if(isset($ourpeople_master)){;?>
                    <input type="text" class="form-control input-sm" name="titlemaster" value="<?php echo set_value('titlemaster', $ourpeople_master->titlemaster);?>" placeholder="Main Title | max 25 characters" required="required">
                </div>
                <div class="col-md-6 col-xs-12">
                    <input type="text" class="form-control input-sm" name="detailmaster" value="<?php echo set_value('detailmaster', $ourpeople_master->detailmaster);?>" placeholder="Main Decription." required="required">
                </div>                  
                <?php }?> 
            </div>      
            <div class="form-group row">                                    
                <div class="col-md-4 col-xs-12">                        
                    <input type="text" class="form-control input-sm" name="name" value="" placeholder="Name Required">
                </div>
                <div class="col-md-4 col-xs-12">                    
                    <input type="text" class="form-control input-sm" name="desig" value="" placeholder="Designation Required">
                </div>
                <div class="col-md-4 col-xs-12">                                                                
                    <input type="file" name="userfile" class="form-control input-sm">   
                </div>
            </div>                                  
            <div class="form-group row">                                    
                <div class="col-md-12 col-sm-12">                       
                    <textarea name='aboutme' rows="1" class='form-control input-sm'  placeholder="About Employee Detail"></textarea>                                
                </div>
            </div>
            <div class="form-group row">                                    
                <div class="col-md-12 col-sm-12">                       
                    <textarea name='certification' rows="1" class='form-control input-sm' placeholder="Employee's Certifications Detail"></textarea>    
                </div>
            </div>  
            <div class="form-group row">                                    
                <div class="col-md-3 col-xs-12">
                    <input type="text" class="form-control input-sm" name="facebook" placeholder="Facebook Profile Link Here">
                </div>
                <div class="col-md-3 col-xs-12">
                    <input type="text" class="form-control input-sm" name="twitter" placeholder="Twitter Profile Link Here">
                </div>
                <div class="col-md-3 col-xs-12">
                    <input type="text" class="form-control input-sm" name="instagram" placeholder="Instagram Profile Link Here">
                </div>
                <div class="col-md-3 col-xs-12">
                    <input type="text" class="form-control input-sm" name="google" placeholder="Google Plus Profile Link Here">
                </div>
            </div>                          
            <div class="form-group row">                    
                <div class="col-md-12 text-right">
                    <button type="submit" class="btn btn-success btn-sm" id="save"><i class="fa fa-save"></i> Save</button>                                     
                </div>                                  
            </div>

1 个答案:

答案 0 :(得分:0)

我觉得你对我的上一个答案感到困惑 - 你可以使用$_FILES['userfile']['name']只需将其包裹在isset或本案empty中,否则通知会触发(并影响你的如果文件没有上传,则json回复。

$config['upload_path'] = 'fassets/images/oupeople';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
//$config['file_name'] = $_FILES['userfile']['name'];     
$config['overwrite'] = TRUE;
//Load upload library and initialize configuration
$this->load->library('upload', $config);

//$ourpeoplesection = ($this->input->post('name')) . ($this->input->post('desig')) . ($this->input->post('aboutme')) . ($this->input->post('certification')) . ($this->upload->do_upload('userfile'));

if (!empty($_FILES['userfile']['name']))
{   
    $this->form_validation->set_rules('name', 'Name', 'required|max_length[25]');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => 'Name Required'));
        exit;
    }           
    $this->form_validation->set_rules('desig', 'Designation', 'required|max_length[20]');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => 'Designation Required'));
        exit;
    }   
    $this->form_validation->set_rules('aboutme', 'About Employee', 'required'); 
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => 'About Employee Required'));
        exit;
    }       
    $this->form_validation->set_rules('certification', 'Certification', 'required');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => 'Certification Required'));
        exit;
    }           

    if(!$this->upload->do_upload('userfile'))
    {
        $error = 'Please upload Image file. <br>';
        //$error = $this->upload->display_errors('', '<br>');
        echo json_encode(array('mes' => 'alert-danger', 'msg' => $error));
        exit;           
    }

    $userData = array (
        'name' => $this->input->post('name'),
        'desig' => $this->input->post('desig'),
        'aboutme' => $this->input->post('aboutme'),
        'certification' => $this->input->post('certification'),
        'facebook' => $this->input->post('facebook'),
        'twitter' => $this->input->post('twitter'),
        'instagram' => $this->input->post('instagram'),
        'google' => $this->input->post('google'),           
        'userfile' =>  $this->upload->data('file_name'),
    );

    //Pass user data to model
    $insertData = $this->Ourpeoplemodel->insert($userData);         
}

我还想建议而不是有许多不同的表单验证条件只有一个......

$this->form_validation->set_rules('name', 'Name', 'required|max_length[25]');
$this->form_validation->set_rules('desig', 'Designation', 'required|max_length[20]');
$this->form_validation->set_rules('aboutme', 'About Employee', 'required'); 
$this->form_validation->set_rules('certification', 'Certification', 'required');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors()));
        exit;
    }