Hi everyone,
I'm building a form to track course data for university students. You can see the layout of the form here:
http://davedelong.com/content/one-form-many-submit-buttons
What I'd like to happen is when the user clicks the "Remove" button corresponding to the course they'd like to remove, then the form is submitted, I determine which remove button was clicked, and remove the appropriate course.
However, submit buttons in FAPI are tied to the #value attribute, and since each submit button has the same #value ("Remove"), I can't tell which button is being clicked.
How can I work around this? I'd like to keep all the "Remove" buttons titled "Remove" but still be able to differentiate between them all.
Thanks,
Dave
Comments
JavaScript
You could change the 'Remove' buttons from Submit buttons to Button buttons and add a hidden field to the form. Use JavaScript to set a click event for each button that (1) sets the value of the hidden field to the course number for the course to be removed, and (2) submits the form. Once you receive the data, just read the value of that hidden field.
Thanks for the idea,
Thanks for the idea, davidmorrow. I'm trying what you suggested, and I've hit a snag.
When I build the form, I declare the Remove buttons like so:
Then I also have this in the form declaration:
When the form is submitted, clicked_course doesn't have a value! I added an "alert($('#edit-clicked-course).val());" in the onclick() event to verify that it was getting the correct value, and it was, but somehow that value isn't getting submitted.
Any ideas why?
Thanks!
Dave
Interesting, I just found
Interesting, I just found the value inside $_POST['clicked_course'], but not in $_GET and not in the $form_values array either.
Odd.
Change #value to #default_value?
I think you'll want to change '#value' to '#default_value' in the $form['clicked_course'] array. According to the API documentation on #value, #value sets a value that cannot be edited by the user, whereas #default_value just sets the initial value. So it's probably getting sent with $_POST and then overwritten on the server side.
That was the piece I was
That was the piece I was missing. It works perfect now! Thanks, davidmorrow! :)
Won't work on Drupal 7
Anyone trying to use this technique for Drupal 7 will find that since the id attribute has been removed from hidden fields in forms, you cannot use $('#edit-whatever') to change the value.
The fix is easy, however, since you can still use jQuery attribute selectors, e.g. $('input[name=whatever]'). Hope this helps anyone trying to use this technique.
I wanted my form to
I wanted my form to gracefully degrade so instead I have each button with a different #value and use CSS to make them all appear the same.
________________________
Dave Hansen-Lange
Director of Technical Strategy, FourKitchens.com
For anyone struggling to find a cleaner solution here...
I wasn't satisfied with any of the proposed solutions above (I don't want to have to resort to JavaScript, or dirty CSS tricks), and finally discovered that if you provide each submit button with a unique
['#name']property, it will work as expected. That is, the chosen button will be properly identified by$form_state['triggering_element'], even it shares the same['#value']as other submit buttons.Again, not sure why I had such a hard time discovering this... it seriously should be documented, but isn't, anywhere. In fact, the
['#name']property on the FAPI reference page is hardly documented at all, even when there's such an important use for it! But anyway, there you go. Hope this helps someone.yes it helped very much.
yes it helped very much. thanks.
Wow, I wasted hours trying to
Wow, I wasted hours trying to get the multiple submit buttons to work properly before finding this simple fix. *Sigh*
Thanks Paul,
Thanks Paul,
This snagged me last night. Simple solution.