Hey,
I've installed Drupal 5.2 a short while ago, and am busy porting my website onto the system, which is going surprisingly well. Anyway, there is one thing that I am stuck on, and I can't figure out an elegant solution to this problem:

I have a form on my website, where users get to fill in some data and furthermore, select an (infinite) number of items out of a long list. It's not a shopping cart, but that might be a useful way to think of it, basically, I have two lists, one containing items a user has selected, together with two input fields per item, specifying say colour and quantity. The other list contains all remaining items, without any further input fields. I have written a Javascript to basically transfer items from one list to the other, simply by clicking on them.

(Furthermore, I have a small text input field above the right list to narrow down choice by regexp matching the contents of that list against the text in this field. - I don't think this should matter too much for my problem)

Well, if the form is submitted, I want to store all the items in the left list together with the values assigned in their respective two fields in a database table [and delete all those items from the table that were moved from the left to the right].

My solution so far has been to create a static page with the form and then to manually update the database once that form has been submitted, however, I would much rather include the whole thing in the proper Drupal API, but am not sure how this would be possible?

I think I would probably have to define a new form type, but I'm not sure if that is the best way to go about this and if it were, exactly how to do so? I looked at the hook_form / form documentation, but didn't find anything that seemed to address this issue - however, if I just missed something obvious here, please point me into the right direction.

Thanks,
Karl Moritz

Comments

karlmoritz’s picture

Just realised that I made this a little complicated. Basically, I am trying to find out how to create an array (or unspecified number) of custom, dynamic, javascript-driven form elements using the Drupal API rather than working around it. Documentation on this issue seems so be a little sparse.

karlmoritz’s picture

Hmm, sorry for having this conversation with myself, but might be useful for other people who might be having a similar problem. I've found a (nearly) elegant solution to my problem now:

In the hook_form(), I use an 'item' type to generate my additional code and fields in:

$form['special'] = array(
	'#type' => 'item',
	'#title' => t('Special field collection'),
	'#value' => _return_specialForm($node),
	);

Everything else is pretty straightforward, except that you need to ensure that Preview and Submit with error work properly, i.e. contain the updated form data for the special fields rather than the old ones from the database. This is done by checking for $_POST["op"] == "Preview" or "Submit" when generating the form, and if either of those are true, fetching field-values from $_POST rather than from the node itself.

Hope that helps if anyone else has the same problem..