Welcome Guest, Not a member yet? Register   Sign In
Newbie best practice advice required, Dynamic pages, validation and set_values
#1

[eluser]netenabled[/eluser]
Hi guys this is my first web project and first MVC codeigniter one. I have virtually finished my site and have it doing everything I need it to do however there are a few places where it falls down and so I was wondering if you could give me an idea of what it is I am supposed to be doing in these circumstances.

I am building a comparison site. My site compares products and then offers then for sale.

I have a homepage with a search. The search submits to the homepage controller.

On successful access to the db via the search_model I return the users to a new view via the homepage controller.

This view is the comparison page. The comparison page submits a form with the details of the chosen product to a new controller, the purchase controller. This in turn saves to the db via a order_model and then returns the user to a new view where the user can make changes to the product that they have chosen.

It is at this point that I have the issue.

Because it is coming from the previous controller, when I use the set_value commands on my radio buttons, they are not setting as the validation for them is in the next controller not the previous one.

Instead of calling this view in the previous controller, should I instead redirect them to the new page and send the information required for that page, that was derived by the previous controller via flashdata?

Would this get around the issue that I am facing.

My overall question is about dynamic pages, validation and set_values.



#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

I read what you posted, and got lost when you came to describing the problem itself. Which controller is it where you're having the trouble. Which is the previous/next controller?

You can save the $_POST data as flashdata if you want. That will work, so long as you have database sessions enabled, and you remember to unpack the flashdata back into the $_POST array. However, I'd suggest doing this quite differently. With your current system, once the form has been submitted, it you hit the back button, the browser will prompt you to resubmit the form.

Try to keep your validation within the controller that served the view to begin with, rather than loading a form with one controller, and submitting it to another. Once validation is successful, you can then redirect to the new controller (which will prevent resubmits when the back button is clicked):

Hompage controller
Code:
if ($this->form_validation->run())
{
    // Do stuff
    redirect('/purchase');
}

I see a lot of people doing the same as you, and I don't have a problem with that, but they do seem to come across this kind of problem often. If you find yourself needing to use flashdata to pass $_POST data to another controller, chances are, it's time to rethink your design.




Theme © iAndrew 2016 - Forum software by © MyBB