codeigniter提交带有文本和图像的表单

时间:2018-01-08 01:01:48

标签: forms image codeigniter text submit

我的提交表单有问题。我提出了一个查询来提交产品,你必须添加有关该产品的信息以及产品的图像。以下是我的代码:

控制器:

<?php
    public function save(){
        $this->load->library('upload');
        $image_path = realpath(FCPATH.'images');
        $config['upload_path'] = $image_path;
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size']     = '2048';
        $config['encrypt_name'] = TRUE;
        $config['remove_spaces'] = TRUE;
        $config['detect_mime'] = TRUE; 



            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('default_img')) {
                $error = array('error' => $this->upload->display_errors());
                print_r($error); 
            } else {
            $data = $this->upload->data();
                print_r($data);
            }


        $this->form_validation->set_rules('nume', 'Nume produs','required');
        $this->form_validation->set_rules('descriere_full', 'Descriere produs','required');
        $this->form_validation->set_rules('scurta_descriere', 'Descriere scurta','required');
        $this->form_validation->set_rules('type', 'Tip produs','required');

        $this->form_validation->set_rules('dimensiuni_1', 'Stock produs','');
        $this->form_validation->set_rules('stock_1', 'Stock produs 1','required');
        $this->form_validation->set_rules('pret_1', 'Pret produs 1','required');

        $this->form_validation->set_rules('dimensiuni_2', 'Stock produs','');
        $this->form_validation->set_rules('stock_2', 'Stock produs 1','');
        $this->form_validation->set_rules('pret_2', 'Pret produs 1','');

        $this->form_validation->set_rules('dimensiuni_3', 'Stock produs','');
        $this->form_validation->set_rules('stock_3', 'Stock produs 1','');
        $this->form_validation->set_rules('pret_3', 'Pret produs 1','');

        // 

        if ($this->form_validation->run()) 
        {
            $data = $this->input->post();
            $data = $this->security->xss_clean($data);
            unset($data['submit']);
            if ($this->Queries->addProduct($data)) {
                $this->session->set_flashdata('msg', 'Produs adaugat cu succes');
            }
            else
            {
                $this->session->set_flashdata('msg', 'Din pacate produsul nu a putut fi adaugat');
            }
            return redirect('Dashboard');

        }
        else
        {
        $this->load->view('admin/template/header');
        $this->load->view('admin/products/create');
    $this->load->view('admin/template/footer');
        }

    }
?>

和我的观点:

    <div class="container" style="margin-top: 50px;">
    <div class="card bg-light mb-3">
      <div class="card-header"><legend class="text-center">Adauga produs</legend></div>
      <div class="card-body">
      <?php echo form_open_multipart('Dashboard/save', ['class' => 'form-horizontal']); ?>
      <fieldset>
        <div class="form-group row">
          <div class="col-sm-10">
          </div>
        </div>
        <div class="form-group">
          <h5 class="card-title">Nume produs</h5>
          <?php echo form_input(['name'=>'nume', 'placeholder'=>'Nume produs', 'class'=>'form-control']); ?>
          <?php echo form_error('nume', '<div class="text-danger">', '</div>'); ?>
        </div>
        <div class="form-group">
          <h5 class="card-title">Descriere scurta</h5>
          <?php echo form_textarea(['name'=>'scurta_descriere', 'placeholder'=>'', 'class'=>'form-control']); ?>
          <?php echo form_error('scurta_descriere', '<div class="text-danger">', '</div>'); ?>
        </div>
        <div class="form-group">
          <h5 class="card-title">Descriere full</h5>
          <?php echo form_textarea(['name'=>'descriere_full', 'placeholder'=>'', 'class'=>'form-control']); ?>
          <?php echo form_error('descriere_full', '<div class="text-danger">', '</div>'); ?>
        </div>
        <div class="form-group">
          <h5 class="card-title">Tip produs</h5>
          <?php 
          $options = array(
                  'saltele_arcuri'         => 'Saltele cu arcuri',
                  'saltele_medicale-spuma'           => 'Saltele medicale-spuma',
                  'saltele_pocket_spring'         => 'Saltele pocket spring',
                  'saltele_memory_foam'        => 'Saltele memory foam',
                  'saltele_latex'        => 'Saltele latex',
                  'saltele_protectie'        => 'Saltele de protectie',
                  'paturi'        => 'Paturi',
                  'canapele'        => 'Canapele',
                  'perne'        => 'Perne',
                  'pilote'        => 'Pilote',
                  'huse_accesorii'        => 'Huse, Accesorii',
                  'lenjerii'        => 'Lenjerii',
          );
          echo form_dropdown('type', $options, 'large', 'class="form-control"');
          echo form_error('type', '<div class="text-danger">', '</div>');
           ?>
        </div>   
        <div class="form-group">
          <h3  class="text-center" style="margin-top: 50px;">Varianta 1:</h3>
          <label>Dimensiuni</label>
          <?php echo form_input(['name'=>'marime_1', 'placeholder'=>'Dimensiunile saltelei', 'class'=>'form-control']); ?>
          <?php echo form_error('marime_1', '<div class="text-danger">', '</div>'); ?>
          <label>Pret</label>
          <?php echo form_input(['name'=>'stock_1', 'placeholder'=>'Stock disponibil', 'class'=>'form-control']); ?>
          <?php echo form_error('stock_1', '<div class="text-danger">', '</div>'); ?>
          <label>Stock</label>
          <?php echo form_input(['name'=>'pret_1', 'placeholder'=>'Pretul produsului', 'class'=>'form-control']); ?>
          <?php echo form_error('pret_1', '<div class="text-danger">', '</div>'); ?>
        </div>
        <div class="form-group">
          <h3 style="margin-top: 50px;"><span class="badge badge-dismissible badge-warning">Optional </span> Varianta 2:</h3>
          <label>Dimensiuni</label>
          <?php echo form_input(['name'=>'marime_2', 'placeholder'=>'Dimensiunile saltelei', 'class'=>'form-control']); ?>
          <?php echo form_error('marime_2', '<div class="text-danger">', '</div>'); ?>
          <label>Pret</label>
          <?php echo form_input(['name'=>'stock_2', 'placeholder'=>'Stock disponibil', 'class'=>'form-control']); ?>
          <?php echo form_error('stock_2', '<div class="text-danger">', '</div>'); ?>
          <label>Stock</label>
          <?php echo form_input(['name'=>'pret_2', 'placeholder'=>'Pretul produsului', 'class'=>'form-control']); ?>
          <?php echo form_error('pret_2', '<div class="text-danger">', '</div>'); ?>
        </div>
        <div class="form-group">
          <h3 style="margin-top: 50px;"><span class="badge badge-dismissible badge-warning">Optional </span> Varianta 3:</h3>
          <label>Dimensiuni</label>
          <?php echo form_input(['name'=>'marime_3', 'placeholder'=>'Dimensiunile saltelei', 'class'=>'form-control']); ?>
          <?php echo form_error('mamrime_3', '<div class="text-danger">', '</div>'); ?>
          <label>Pret</label>
          <?php echo form_input(['name'=>'stock_3', 'placeholder'=>'Stock disponibil', 'class'=>'form-control']); ?>
          <?php echo form_error('stock_3', '<div class="text-danger">', '</div>'); ?>
          <label>Stock</label>
          <?php echo form_input(['name'=>'pret_3', 'placeholder'=>'Pretul produsului', 'class'=>'form-control']); ?>
          <?php echo form_error('pret_3', '<div class="text-danger">', '</div>'); ?>
        </div>
        <div class="form-group" style="margin-top: 50px;">
          <?php echo form_upload('default_img'); ?>
          </form>
        </div>
        <div class="text-center">
          <?php echo anchor('Dashboard', 'Back', ['class'=>'btn btn-primary']); ?>
          <?php echo form_submit(['name'=>'submit','value'=>'Submit','class'=>'btn btn-primary']); ?>
        </div>
      </fieldset>
  <?php echo form_close(); ?>
      </div>
    </div>
</div>

当我在视图标题中提交带有form_open_multipart的表单时,我从控制器获取上传到指定文件夹中的照片,但在db字段中没有任何内容,当我在视图标题中提交带有form_open的表单时,我将获得所有信息字段,包括我提交为照片的文件名,但未上传。 可以某人

1 个答案:

答案 0 :(得分:1)

制作上传方法

    /**
         * Upload a file.
         *
         * @param $field
         * @param string $allowed_types
         * @return bool
         */
        public function do_upload($field, $allowed_types = 'jpg|png|gif')
        {
            $config['upload_path']          = realpath(FCPATH . 'images');
            $config['allowed_types']        = $allowed_types;
            $config['max_size']             = 500;
            $config['max_width']            = 2048;
            $config['max_height']           = 1024;
            $config['encrypt_name']         = TRUE;
            $config['remove_spaces']        = TRUE;
            $config['detect_mime']          = TRUE;

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload($field))
            {
                return FALSE;
            }
            else
            {
                $data = $this->upload->data();

                return $data["file_name"];
            }
        }

制作验证方法

   /**
     *Validate the form.
     */
    public function prepareValidation()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('nume', 'Nume produs','required');
        $this->form_validation->set_rules('descriere_full', 'Descriere produs','required');
        $this->form_validation->set_rules('scurta_descriere', 'Descriere scurta','required');
        $this->form_validation->set_rules('type', 'Tip produs','required');

        $this->form_validation->set_rules('dimensiuni_1', 'Stock produs','');
        $this->form_validation->set_rules('stock_1', 'Stock produs 1','required');
        $this->form_validation->set_rules('pret_1', 'Pret produs 1','required');

        $this->form_validation->set_rules('dimensiuni_2', 'Stock produs','');
        $this->form_validation->set_rules('stock_2', 'Stock produs 1','');
        $this->form_validation->set_rules('pret_2', 'Pret produs 1','');

        $this->form_validation->set_rules('dimensiuni_3', 'Stock produs','');
        $this->form_validation->set_rules('stock_3', 'Stock produs 1','');
        $this->form_validation->set_rules('pret_3', 'Pret produs 1','');

        return;
    }

现在调整你的保存方法

public function save()
    {
        $data = array();

        if ($_POST) {
            $this->prepareValidation();
            if ($this->form_validation->run()) {
                $data = $this->input->post();
                $data = $this->security->xss_clean($data);
                unset($data['submit']);

                if($data['default_img'] = $this->do_upload('default_img')){ // You should have a field name default_image in database table also. Or make a variable $data['table_field_name_where_you_want_to_store_image']
                    if ($this->Queries->addProduct($data)) {
                        $this->session->set_flashdata('msg', 'Produs adaugat cu succes');
                        return redirect('Dashboard');
                    } else
                        $this->session->set_flashdata('msg', 'Din pacate produsul nu a putut fi adaugat');
                }
                else {
                    $data['errors'] = array('error' => $this->upload->display_errors()); // Print these error in view.
                }
            }
        }

        $this->load->view('admin/template/header');
        $this->load->view('admin/products/create');
        $this->load->view('admin/template/footer');

        return;
    }