0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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!
Well, you got it. I've been struggling with using URIs with this for 3 days. Hidden input is the way to go.
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.
0

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 }
}

2 Comments

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!
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.