Welcome Guest, Not a member yet? Register   Sign In
Posting a form that is included in a page
#11

[eluser]JamieBarton[/eluser]
Hi again,

Also forgot to ask.

Say I have a form to create a message, the URL being /messages/new

Instead of having two controllers, one to handle including the view, and one to post the actual content, such as /messages/create

How would I go about putting it all in one controller? Like if the form is submitted ... Do this, if not, show this etc. I'm probably just being stupid here, as it seems very easy and could do it in Procedural PHP fine.


Thanks for your help.
#12

[eluser]Flemming[/eluser]
Hi Jamie,

the answer is above! But here it is again ...

Code:
if($this->form_validation->run())
        {
            // validation passed, so do something with the data and then probably redirect
        }
        else
        {
            // validation failed OR form not posted yet
            $this->load->view('view',$data);
        }
#13

[eluser]JamieBarton[/eluser]
Ah, I thought so! I wasn't sure if the form validation run() meant when the form was submitted. I'm stoopid!
#14

[eluser]dhenoer[/eluser]
Comment to post#7
According your codes:
Code:
function post_to_wall()
    {              
            //set validation rules
            
            if($this->form_validation->run() == TRUE) {

                //save data to database                
                
            } else {
                
                //I will make a comment about this
                $data['recent_wall_posts'] = $this->wall->get_wall_posts(5);
                $this->load->view('dashboard/wall', $data);
                
            }
    }

If you use set_value() function, passing $data into the view('dashboard/wall', $data) will re-populate the form gotten from database again.. So user should miss where my text I've submitted is?

:red: Oppsss sorry.. forget my comment.. I think you use initial value that retrieved from database for your form..
#15

[eluser]InsiteFX[/eluser]
You can also create a Forms Library.
Code:
class Forms {

    /**
     * -----------------------------------------------------------------------
     * Class variables - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * The CI object
     */
    private $CI;

    // -----------------------------------------------------------------------

    /**
     * __construct()
     *
     * Constructor    PHP 5+    NOTE: Not needed if not setting values!
     *
     * @access    public
     * @return    void
     */
    public function __construct()
    {
        $this->CI = get_instance();

        $this->CI->load->helper(array('form', 'url', 'email'));
        $this->CI->load->library('form_validation');

        log_message('debug', 'Class Forms Initialized');
    }

    public function form_1()
    {
        // setup your form rules here!

        if ($this->CI->form_validation->run() == FALSE)
        {
            $this->form_view('your_form');
        }
        else
        {
            // process the form data
            $this->form_view('your_success_form');
        }
    }

    // -----------------------------------------------------------------------

    /**
     * form_view()
     *
     * Load a specific form view.
     *
     * @access    public
     * @param     string - $page
     * @param     mixed  - $params
     * @return    void
     */
    public function form_view($page, $params = NULL)
    {
        if ($params !== NULL)
        {
            $data['data'] = $params;
        }

        $data['page'] = $page;

        $this->CI->load->view('your_view', $data);
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB