0

I am very new to codeigniter and I am currently working on a register form. For the moment I just want the user to input their first name and a picture. However, I have 2 different controllers to handle this and I have no clue of how to merge them into a single form without having two different submit buttons.

View

<div class="container">
  <div class="row">
      <section class="page col-md-8">

        <h2 class="page-title">Registration Form</h2>


        <form method="post" id="expertiseForm" action="" >

        <?php echo $this->session->flashdata('msg'); ?>

            <?php $attributes = array("name" => "expertiseform");
            echo form_open("index.php/expertise/index", $attributes);?>

              <div class="form-group required">
                    <label class="control-label" for="fname">First Name&#160;</label>
                    <input class="form-control" name="fname" type="text" value="<?php echo set_value('fname'); ?>" />
                    <span class="text-danger"><?php echo form_error('fname'); ?></span>
              </div>

              <div class="form-group"> 
                  <button type="submit" class="btn btn-default" value="form-submit'">Submit</button>
              </div>

        <?php echo form_close(); ?>
        </form>


        <!-- Upload form -->
        <?php echo form_open_multipart('index.php/upload/do_upload', array('id' => 'uploadForm'));?>

          <div class="form-group required">
            <label class="control-label" for="filephotograph">Photograph:&#160;</label>
            <input type="file" class="form-control-file" id="filephotograph" name="filephotograph" aria-describedby="fileHelp" value="<?php echo set_value('filephotograph'); ?>">
            <small id="fileHelp" class="form-text text-muted">Upload a photograph smaller than 2 MB in size.</small>
            <span class="text-danger"><?php echo form_error('filephotograph'); ?></span>
          </div>

            <input type="submit" value="upload" />

        <?php echo form_close(); ?>
        </form>
    </section>
  </div>
</div>

Controller for first name

<?php
class Expertise extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('expertise_model');
    }

    function index()
    {
        $this->load->view('templates/navbar');
        $this->load->view('templates/header');

        $this->form_validation->set_rules('fname', 'First Name', 'trim|required|alpha|min_length[2]|max_length[30]|xss_clean');


        if ($this->form_validation->run() == FALSE)
        {
        // fails
            $this->load->view('expertise_form');
            $this->load->view('templates/footer');
        }

        else {

            $data = array(
                'fname' => $this->input->post('fname')
                );

            if ($this->expertise_model->insert_expertise($data))
            {
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">You have successfully submit your expertise form for review.</div>');
                redirect('index.php/expertise/index');
            }
            else
            {
            // error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Error. Please review your information and try again.</div>');
                redirect('index.php/expertise/index');
            }
        }
    }
?>

Controller for uploading photos

<?php

class Upload extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                $this->load->helper(array('form', 'url'));
        }

        public function index()
        {
                $this->load->view('upload_form', array('error' => ' ' ));
        }

        public function do_upload()
        {

                $config['upload_path']          = './application/CAHSIfiles/uploads/photos';
                $config['allowed_types']        = 'gif|jpg|png';
                $config['max_size']             = 100;
                $config['max_width']            = 1024;
                $config['max_height']           = 768;

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

                if ( ! $this->upload->do_upload('filephotograph'))
                {
                        $error = array('error' => $this->upload->display_errors());
                        redirect('index.php/expertise/index');
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());
                        redirect('index.php/expertise/index');
                }
        }
}
?>

1 Answer 1

1

just have one set of form tags with the url putting to one function that process your first set of form items and then does the actual upload logic

<?php echo form_open_multipart('index.php/index.php/expertise/proccess_profile', array('id' => 'uploadForm'));?>

    <div class="form-group required">
       <label class="control-label" for="fname">First Name&#160;</label>
       <input class="form-control" name="fname" type="text" value="<?php echo set_value('fname'); ?>" />
       <span class="text-danger"><?php echo form_error('fname'); ?></span>
    </div>

   <div class="form-group required">
        <label class="control-label" for="filephotograph">Photograph:&#160;</label>
        <input type="file" class="form-control-file" id="filephotograph" name="filephotograph" aria-describedby="fileHelp" value="<?php echo set_value('filephotograph'); ?>">
        <small id="fileHelp" class="form-text text-muted">Upload a photograph smaller than 2 MB in size.</small>
        <span class="text-danger"><?php echo form_error('filephotograph'); ?></span>
      </div>

   <div class="form-group"> 
      <button type="submit" class="btn btn-default" value="form-submit'">Submit</button>
   </div>



 <?php echo form_close(); ?>

Controller

public function process_profile(){
    //logic from your upload controller and other controller here
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry that I took too long to give a reply, it took me a while to figure it out but I managed it. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.