Welcome Guest, Not a member yet? Register   Sign In
validation loop and back button, and wont wear a wig !!
#1

[eluser]tim1965[/eluser]
Hi

I have a set of registration forms that are working correctly with validation, each form validates correctly and redirects. The data for each form is stored in a temp table and then at the end of the process there is a master save that moves all the data to the live tables.
I want to add a back/previous form button that will allow the user to go backto the previous form. This will essentiallt involve looking up the data from the previous form and populatin gthe relevant fields.
I have found an approach to this using
//HTML
<input type="submit" value="Save" name="submit_action" />
<input type="submit" value="Save and Finish" name="submit_action" />

//PHP
$action = $this->input->post('submit_action');

if($action == 'Save') {
...
} else if($action == 'Save and Finish') {
...
}
This would essentially allow me to hit the back button to take me back to the previous form calling the db query and then populating the form using set_value's default.
So my problem is trying to integrate this funcionality on the source form i.e. one with the back button.
The structure of my controller is essentially as per the user guide, i wont post the actual controllers due to size (but the structure is essentially the same).
class Form extends Controller {

function index()
{
$this->load->helper(array('form', 'url'));

$this->load->library('form_validation');

$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');

if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}

However i am struggling to get these to work together (bad day!!). As i cant work out where to place the submit functionality. I have tried this lots of different ways but essentially come back to validation run and form load need to come first, which gives me a problem grabbing the $_post submit value. Please help me save what little hair i have. Thanks.
#2

[eluser]TheFuzzy0ne[/eluser]
Which back button are you referring to? One you've added, or the browsers back button? If you're talking about the browsers back button, it won't work for two reasons:

1) Unless you redirect after each form submission, you'll end up with the user submitted data more than once without doing it intentionally. Also, they will see an annoying popup asking them if they're sure they want to resubmit the form.
2) Cache. As far as I'm aware, your browser will call on a cached page when you use the back button.

However, this is your solution in a way. If you're adding your own custom back and next buttons, the value attribute for those buttons can be set to the appropriate page number, so that you app knows which page the user is currently on.

Hope this helps.
#3

[eluser]tim1965[/eluser]
Fuzzyone thanks for your response. I want to add a custom back button. When the user moves from form 1 to form 2 the submit will save their data into a temp table. On form 2 i want to give the user the ability to move back to form 1 with a back button. When the user hits the form 2 back button i do a lookup to the temp table for the saved data for form 1 and call form 1 and populate the fields with the saved datai.e.add\edit ability. So the problem i am struggling with is integrating the approach from this thread http://ellislab.com/forums/viewthread/96514/ with the standard validation controller as per my first post.
Thanks
#4

[eluser]TheFuzzy0ne[/eluser]
I would suggest always submitting the data to the same controller, and exporting the functionality of each page to individual private methods.

Here's my idea. Now I haven't done it all for you, there are a lot of blanks, but I think it will work.

Code:
<?php

// You'll need to add a route for this controller
// $route['form/(.+)'] = 'form/index/$1';

class Form extends Controller {
    
    function Form()
    {
        parent::Controller();
        $this->load->library('form_validation');
        $this->load->helper('url');
    }
    
    function index()
    {
        if ($this->session->flashdata('form_complete') === TRUE)
        {
            $this->_success();
        }
        else {
            
            $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 1;
            
            if ($page == 1)
            {
                $this->_page1();
            }
            else if ($page == 2)
            {
                $this->_page2();
            }
            else if ($page == 3)
            {
                $this->_page3();
            }
            else
            {
                $this->_success();
            }
        }
    }
    
    function _page1()
    {
        // Set any validation rules here
        
        if ($this->input->post('submit') && $this->form_validation->run())
        {
            // Add data to database.
            
            if ($this->input->post('next'))
            {
                redirect('form/2');
            }
        }
        
        // Set any errors for the view here.
        
        $this->load->view('page1');
    }
    function _page2()
    {
        // Check that the preceding page has been submited. If it has, then
        // It's been validated and so is correct. If it's not, redirect to that
        // page. You can set a flash data message here if you want, to be displayed
        // by the previous controller method.
        
        // Set any validation rules here
                
        if ($this->input->post('submit') && $this->form_validation->run())
        {
            // Add data to database.
            
            if ($this->input->post('prev'))
            {
                redirect('form/1');
            }
            else
            {
                redirect('form/3');
            }
        }
        
        // Set any errors for the view here.
        
        $this->load->view('page2');
    }
    
    function _page3()
    {
        // Check that the preceding page has been submited. If it has, then
        // It's been validated and so is correct. If it's not, redirect to that
        // page. You can set a flashdata message here if you want, to be displayed
        // by the previous controller method.
        
        // Set any validation rules here
                
        if ($this->input->post('submit') && $this->form_validation->run())
        {
            // Add data to database.
            
            if ($this->input->post('prev'))
            {
                redirect('form/1');
            }
            else
            {
                $this->session->set_flashdata('form_completed', TRUE);
                redirect('form');
            }
        }
        
        // Set any errors for the view here.
        
        $this->load->view('page3');
    }

    function _success()
    {
        // Move the data from the temp table to the actualy table.
        
        $this->load->view('success');
    }
}
#5

[eluser]tim1965[/eluser]
Thanks will work on this now




Theme © iAndrew 2016 - Forum software by © MyBB