Welcome Guest, Not a member yet? Register   Sign In
loading view after form validation
#1

[eluser]Pale Rider[/eluser]
I'm new to CodeIgniter, been using it, digging through the forums/docs for the past 2 weeks and so far I'm loving it.

I'm a regular Wordpress junkie usually, but obviously Wordpress only works in certain situations. I am finding the power and flexibility of CodeIgniter to be very rewarding.

My (simple) question is about loading a few views.

I have a view that contains a form. All the inputs are using set_value to keep the values in case of an error on validation.

Code:
<?php

class Sales extends Controller {
    
    function index()
    {    
        $this->load->view('tpl_header');
        $this->load->view('sales_entry_view');
        $this->load->view('tpl_footer');
    }
    
    function add()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('order_number', 'Order Number', 'trim|required|numeric')
                // a bunch more set_rules yada yada

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('tpl_header');
            $this->load->view('sales_entry_view');
            $this->load->view('tpl_footer');
        } else {
            $this->load->model('site_model');
            if($query = $this->site_model->add_order())
            {
                //$this->load->view('tpl_header');
                //echo "WIN!";
                //$this->load->view('sales_entry_view');
                //$this->load->view('tpl_footer');
                redirect('/sales');
            } else {
                $this->load->view('tpl_header');
                $this->load->view('sales_entry_view');
                $this->load->view('tpl_footer');
            }
        }
    }
}

My question is about when the form validation passes. Originally I tried to reload the form view, but when it reloads, it still has all the values. Is there any way to reload the sales_entry_view and clear the inputs/set_values? I found out that if I do a redirect (as you see above) I get a clear/blank form. I'm not sure if this is the proper method to go about this...

My second question is, on a successful submit I want to display "Success!" message. Do I need to load a view before the form such as 'sales_entry_sucess'?

Originally I tried:

Code:
} else {
                                $data['success'] = TRUE;
                $this->load->view('tpl_header');
                $this->load->view('sales_entry_view', $data);
                $this->load->view('tpl_footer');

Then seeing if $success is true, show the success message. Problem with that is I get notice warnings when the view is loaded and success isn't defined.

I'm sure it's something simple, I'm just set. I've searched through the forums and user guide and haven't found anything that quite answers my questions.

Any help would be appreciated.

cheers
#2

[eluser]Craig300[/eluser]
At the top of the function where you're setting the validation rules, you could try defining the variable to get rid of that problem. Something like:-

Code:
$data['success'] = FALSE;

Hope this helps Smile

Craig
#3

[eluser]Pale Rider[/eluser]
Thanks, craig. Idk why I didn't think of that heh.

The problem is using the redirect method (code above) I can't pass any variable.

Anyone know how to reload a form/view but clear the input fields after a successful submit?
#4

[eluser]danmontgomery[/eluser]
After a successful submit:

Code:
$_POST = array();
#5

[eluser]Pale Rider[/eluser]
Ok, where does this need to go? I've tried doing

Code:
if($query = $this->order_model->add_order())
            {
                $_POST = array();
                $data['success'] = TRUE;
                $this->load->view('tpl_header');
                $this->load->view('sales_entry_view', $data);
                $this->load->view('tpl_footer');
            } else {

And I've tried doing it within the view when $success is true

Code:
if ($success == TRUE)
    {
         echo "Success!";
        $_POST = array();  
    }

Both instances still result in the input fields being populated with the submitted information.




Theme © iAndrew 2016 - Forum software by © MyBB