Welcome Guest, Not a member yet? Register   Sign In
MVC And Form Submission Best Practice
#1

[eluser]esingley[/eluser]
Hello!

I've been doing PHP development for a while but am new to MVC and am trying to get my head around the basic concepts. Here's what I'm doing now: I have a form that when submitted will insert the form data into a DB, redirect back to the original form, but this time with a "Information saved." message at the top.

So, I've got something like this in my Controller:
Code:
class My_Controller extends Controller {
    function My_Controller()
    {
        parent::Controller();
        $this->load->helper('url');
    }

function index()
{
        
        $data['save_msg'] = "";
        if ($this->uri->segment(3) == "s") {
            $data['save_msg'] = "Information saved.";
        }
        
        $template['main_content'] = $this->load->view('invoice_view', $data, true);

        $this->load->view('main_template_view', $template);
        
}
    
function save()
{
               // some code to insert data into DB will go here, then on successful insertion:
        redirect('invoice/index/s');
}

}

But this leaves with with 1. a funny URL after saving and 2. the thought that there's probably a better way to do this. Smile Is there? I know this is basic, but I want to get these basics down before I move on!

Thanks for the help!

~ Eric
#2

[eluser]schnoodles[/eluser]
OK for starters i would use that because i could just type in the url with /s in it.

I tend to do
Code:
class Blog extends Controller {

function index() {
   //rules

  if ( $this->validation->run() == false ) { //show form again }
  else { set_flashdata('success'); redirect('blog/success');

}

function success() {
//show different view and echo flashdata where you want it.
}

}
#3

[eluser]esingley[/eluser]
I almost get it. So, in index(), I'd also show the view before any form submission took place, yes?

And set_flashdata - what is that doing exactly? Does this enable me to use the same view for the pre-validation form and the form after success?
#4

[eluser]schnoodles[/eluser]
If you are using the validation library

Code:
if ( $this->validation->run() == false ) { $this->load->view('asdasd'); }

That will show the view if you submit or if you dont submit, so it handles both.

And to do with set_flashdata its part of DB_Session if you are using that, it stores the flash in the database so when you do a redirect you can access it then it gets rid of it.

Although if you point to a function success() { } you can just add the success message in there manually seeing its a whole success page.
#5

[eluser]esingley[/eluser]
Thanks! This does make sense. But, I have one more curve ball. I'd like to use the same form for editing. So, there will be a case where I'll load the form prepopulated with data. In this case, the validation would return true, but it's not a "save," so I wouldn't want to show the successful save message.

Is there perhaps another approach?

Thanks!
#6

[eluser]Developer13[/eluser]
The validation would only return true if a POST has occurred. You can load the data, pass it to the view, populate the form, and have your controller run validation without it actually validating as long as there has not been a POST event.




Theme © iAndrew 2016 - Forum software by © MyBB