Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Bill Dowd
Bill Dowd
7,681 Points

Submit multiple forms to multiple database tables with one submit button

Try to imagine writing a check in Quickbooks. You fill out a normal check form, but below it there are multiple splits, places to expense the check to. There may be several split entries, but always at least one. The check needs to go to one DB table and each split needs to go to another table, multiple records and not losing the reference to the check, which means the ID for the check must be included as a foreign key in the splits table. This must be done with a single submit button. I understand when you submit 2 forms directly, you will probably succeed with the first from, but the second form will be lost unless special processing, like combining the forms somewhere, takes place. This is pretty complicated, but critically important for my app. I'd love to include a screen shot of what it will be modeled after, but it doesn't appear that I can do that here.

Another problem is the split form field names and multiple split records. Normally, my field names are identical to the DB table, field names. I think I can work around that, but for sure, it could be a big obstacle.

Googling so far, has not given me the answer to such a complicated, specific request. I know there are some very clever people on this forum and I hope one or many of you will jump in to help me with this.

Thanks, Bill

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

So what I would do here is look at how the most popular frameworks today are doing this. Rails has a built in way to handle this type of scenario, allowing nested form attributes. When the form is set up correctly, the html that Rails outputs might look like this:

Let's say you had two tables: checks and expenses.

Any input for the checks table would simply have a name="checks[to]" for the To field of the check, and in the same form would have a name=checks[expenses][1] and name=checks[expenses][2] for the next line item, or whatever you wanted to name them, I don't know, and the process doesn't change.

It seems that Laravel, written in PHP has a similar ability, found here: http://stackoverflow.com/questions/20684932/nesting-models-relations-in-forms-laravel

I realize you might not want to use Rails or Laravel, but you can learn a lot from how they do it, and extract a similar situation to fit within your own ecosystem.

I will say what you want to do isn't the easiest, but it can be done. What I will encourage you to do is to find a solution n where this is just one form that submits the data to the appropriate tables, that would be the most correct, and valid way to do this.

Bill Dowd
Bill Dowd
7,681 Points

Thanks Kevin. I'll take a look at Ruby. I'm not really sure how to investigate a framework for what it does, so I hope I will have success.