Show forms in a modal window
I have a form and I am using Drupal 7. I need to show this form in a popup when a user clicks a link. The user should then be able to complete the form whilst in the pop up window. I can use anything like Colorbox or modals or anything. I just wanted to know which is the better option and what options do I have for Drupal.
Answers 5
There's 2 good options I know of currently: iframe (in colorbox for example) and CTools. Which option to use depends of the circumstances. I guess this information I found in the CTools modal.html file brings out the main difference:
I have no personal experienced with CTools on this subject so I can't add anything else to that, but I've implemented the iframe method in a couple of projects. In the most recent one I used the Colorbox plugin to show a few forms created with the Webform module in a popup. I'll add some example code here in case there's some interest.
Link to the form:
Javascript code to open the linked address in a popup:
In the theme template file:
I attached 'colorbox=true' to the links using javascript so that users with javascript turned off would see the form with the normal template. The iframe template only has messages, title and content printed.
This already works, but I ran into a issue with Webforms: 'colorbox=true' wasn't preserved when the form got submitted. My attempt to fix it:
Use CTools which can insert a form into a modal when a user clicks a link.
Please check the following tutorial: Insert a form into a pop-up modal with CTools and Drupal 7 which simplifies this process in few steps.
Basically you need to define your
hook_menu()
callback for your modal form:then create a link generator which returns the HTML code:
so it can be used in your page callback, e.g.:
When user clicks on the link, it makes request either to
/mymodule/ajax
or/mymodule/nojs
(in case ofnojs
), so the following callback handles creating a modal:Now you just need to create a form and its submit handler as below:
To test that, go to:
/mymodule/page
where you should see 'Magical Modal' link which should show you the modal form once clicked.I would start looking at Modal form, instead of Colorbox. It specifically exists because using Colorbox with forms works really bad.
In modal form, Ctools does all the work in background, which has proper support for form handling that just doesn't belong in Colorbox. That also means that if you need to "modal" an unsupported form then you always know that thanks to Ctools there's a solid API behind it that you can switch to instead.
Bonus points for filing a patch with new form support if you write it. ;)
I find that Simple Dialog is a great way to provide forms in Modals. It has the advantage of using jQuery UI which is in core.
All you need to do is provide a link to the form with some additional information. It provides a simple method based on classes and rel tags, or a theme method for more fine-tuned control. I've done this in two ways:
The module you need is https://drupal.org/project/popup_forms, but you need to do some programming in order to apply it (i.e. it can't be just configured via admin interface).