Hi, I am using Forms API to build a page which displays a table of rows from a DB, with a checkbox on each row. At the bottom is a Submit button. I can render it but the submitted value is always that of my last checkbox. This tells me that I am probably overwriting part of my forms array, or the HTML is not submitting properly. It's probably fixable, but my larger question is: what is the easiest and best way to do this? Eventually I will want to page the output, create filters, sorts, etc. so I'd like to be "smart".
I've read a lot of ways to create pages like this:
* iterate over the rows and add to form[box] with each row
* create separate forms for each row
* use Javascript
* use CCK
I would prefer to not load tons of modules. Thanks for any input.
Comments
Definitely do not create a
Definitely do not create a form for each row, that sounds a terrible waste. I think you may need to use the form tree attribute I believe I had this problem when creating something similar in the past that should stop all your stuff from getting overwritten, that hopefully gives you a little something to go on to start with.
FiveRDesign - UK Drupal Consulting, Development, Custom Modules & Support
MySweetShadow - Heavy Metal Blog, Drupal & SEO
Thanks for the quick
Thanks for the quick response. Here's the code I'm using so far:
It's working OK, but I'm creating submit buttons for every checkbox - not optimal, I'm trying to do multiple deletes based on checked boxes. If I try to create the submit button with another constructor (since I am creating the checkboxes in a while loop), I can't get the selected checkboxes from the form_values array. I also tried adding $qid as an argument to the #submit value callback for the Submit button, no luck there. Finally, I tried conditional logic in the queue_checkbox constructor, also no luck. So really what I'm missing is how to have one submit button send the values of all checked boxes on the page (is this possible?). Thanks.
<?phpfunction
Contact me to contract me for D7 -> D10/11 migrations.
Excellent, thanks. Creating
Excellent, thanks. Creating "n" checkboxes is accomplished by passing a row count to the checkbox_form constructor. Validation works great. The only problem is how to render the individual checkboxes in a table, with one checkbox next to each DB row. Looks like there are some CSS hacks that might work. I can't seem to render the boxes one at a time since they are all one form. Also the forms API for Drupal 6.14 seems a bit buggy, no?
The forms API is great - it
The forms API is great - it just takes a bit of getting used to. But I've not yet found anything I couldn't do with it.
As for how to put your checkboxes in a table, you have to run it through a theming function:
There you go. It's not the easiest thing to do, but it works really well and you end up with a nicely formed table that will have a sticky table header if the form is longer than can fit into one screen. See theme_table() at api.drupal.org for more info on building HTML tables in Drupal.
Contact me to contract me for D7 -> D10/11 migrations.
I never would've figured this
I never would've figured this out... thanks. I couldn't quite get the theming to work, mostly WSOD even with PHP errors turned on. We must be using different drupal versions since the DB calls have changed a bit. I ended up working around the problem by using some global variables - accessing the $form array all over the place became problematic, so I hacked around it. When I get my code all cleaned up, I will post here.
The method I posted is for
The method I posted is for all of Drupal 6.x. The database call was just a typo on my part - I typed it off the top of my head and missed 'FROM'. But it wasn't meant to be a real case, just an example. If you were getting WSOD, it was probably a problem with not finding theming functions. Clear your cache, and also visit admin->build->themes when that happens. This will rebuild your theme registry.
You really shouldn't need to be using global variables at all. This method is the method they use in the core (admin->manage contents->content and admin->manage users -> user), and I use it regularly in modules. Go back and compare what you've done with what I showed you, and you will probably just find you made a typo somewhere along the way.
Contact me to contract me for D7 -> D10/11 migrations.