views:

42

answers:

2

How do I submit a form that can do two different things based on the URI?

For example, if the URI contains a string "new" the form will submit differently than it would if "new" were not in the URI.

I'm having trouble implementing this, as when a form is submitted, it takes the URI of whatever "form_open" says.

+1  A: 

Altering the form_open path is probably not the way to do this. How are you using this? Does the person filling out the form affect the "new" string?

What I would do is put a hidden input on the form and set THAT value to "new". Then in the controller, use a GET to take the value of the input form, and do a simple IF / ELSE statement based off the value of that variable.

This way, you could setup several different ways to use the same form - hidden=new, hidden=old, hidden=brandnew, hiddend=reallyold could all process the form values differently, even sending them to different tables in your DB or whatever.

eddt
Basically, it's a HUGE test--I use "new" for new users, and "update" for users who just need to update some information. Hidden input the only way?
Kevin Brown
Well no, its not the only way... you could do something like you were thinking originally. You could make two paths in your routes.php, such as path/to/script/new and path/to/script/update - then, build just one single form view, but alter or manipulate the contents that you need to based on whether or not the form is loaded via the "new" or "update" path... That's probably your simplest solution actually!
eddt
Well, you got it. I've been struggling with using URIs with this for 3 days. Hidden input is the way to go.
Kevin Brown
Yep, I use hidden input too. Most often you include an ID in your submissions anyway, I often use zero to mean 'create' and valid to mean 'update'; most of the code can remain the same so the if/else just wraps around an insert/update command.
Kurucu
A: 

Kevin - I thought I'd done something like this before and I had - here's a quick look:

In routes.php:

$route['some/pathname/(:any)'] = "my_controller/my_function/$1";

Then in mycontroller.php:

function my_function($type)
{
    if ($type == "new") { 
        do this }
    elseif ($type == "update)" { 
        do this }
}
eddt
Right, eddt. But what happens when my form is submitted? Depending on my form_open('location'...), the URI changes, right? That'll cause a small problem!
Kevin Brown
Well typically when I'm using CI, I create an additional route for my form to submit to, then point the form to that part of the controller, like "some/formsubmit" (may or may not make a new controller for the form submittal process, depends on the need) - that way, regardless of where the submit comes from ("new" or "update") they all go to the same submit place! And since you've variableized the TYPE, you can also pass that in the form_open string...
eddt