Welcome Guest, Not a member yet? Register   Sign In
Dynamic Form with saving post data
#1

[eluser]carlhussey[/eluser]
I am pretty new to codeigniter / OOP but I am giving it a shot and a little stumped at this point.

I have 2 functions. One is the search which loads data from a model. The other is the save function.

My issue is, when running save() I am getting errors because its trying ti display the validation errors but we no longer have the data from the database that we got from the search() function.

I feel that it would be redundant to include all the details form the search back into this save function which is why I think I am doing something wrong.

Code:
public function search()
    {

        // Define some vars
        $data['title'] = 'Submit Attrition';
        $data['js_file'] = 'submit.js';

        // Load our helper
        $this->load->helper('form');

        // Get the user and pass it to the model
        $empQID = $this->input->post('empQID');  
        $data['userDetails'] = $this->submit_model->get_details($empQID);
        $data['languages'] = $this->submit_model->get_languages();
        $data['types'] = $this->submit_model->get_types();
        $data['ratings'] = $this->submit_model->get_ratings();
        $data['processes'] = $this->submit_model->get_processes();

        // Send the data to the views      
        $this->load->view('templates/header', $data);
        $this->load->view('submit/search', $data);
        $this->load->view('templates/footer', $data);
    }

    /**
    * Validate & save attrition submission
    *
    * @author   Carl
    * @return   void
    */
    public function save()
    {
        $data['title'] = 'Submit Attrition';
        $this->load->library('form_validation');
        $this->form_validation->set_rules('language', 'Supporting Language', 'required');

        // Validation failed, show form w/ validation errors
        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('submit/search', $data);
            $this->load->view('templates/footer', $data);
        }
        else
        {
            // Success : Send data to model
            $this->submit_model->save_attrition();
            $this->load->view('templates/header', $data);
            $this->load->view('submit/success', $data);
            $this->load->view('templates/footer', $data);
        }  
    }
#2

[eluser]RobertSF[/eluser]
I'm having a bit of a problem understanding what you're doing. It looks like search() pulls data out of a database for a specific empQID and displays it in a view, view which is a form with a submit control. Then the user submits that form, and save() takes over and saves the date (obviously in some transformed way). It looks a little like "cancel order" or "delete user," where you enter an order number of a user id, and after reviewing the data for that order or user, you press a confirm button to actually go ahead and cancel or delete.

If that's the situation, you can solve it with one function and one view that is a form with two submit controls, one to search, and one to confirm the operation.

If that's not the situation, and your problem would be solved if only the $data from search() were available in save(), you can accomplish that by saving $data to the session in search()
$this->session->set_userdata('data' => $data);
and then retrieving it in save()
$data = $this->session->userdata('data');
$this->session->unset_userdata('data');


Or you could use flashdata, which is a bit more convenient, though keep in mind the CI manual says the limit to the information passed through the session is under 4k, so your data is larger than that, you'll have to use native session for that. Native sessions have no arbitrary limit to how much data they can handle, and they're not hard to use. http://us3.php.net/manual/en/book.session.php




Theme © iAndrew 2016 - Forum software by © MyBB