Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Drupal 6: Inserting a form in a lightbox window and submitting it with AJAX
The purpose of this code snippet is to show you how you can use the lightmodal functionality in the Lightbox2 module to embed a form in a fancy css popup window. But why would you want to do this? Because it looks great and it improves usability.
The first thing you’ll need to do is register a menu item for the page callback that you’d like to appear in the lightbox window.
Next define a function to create the form:
Next add a link to your page content to open up the lightbox window with your new form in it. By adding the rel=lightmodal property onto an A tag, you’ll automatically load the contents of the href in a fancy lightbox window.
I made my lightbox form more usable by using ajax to submit the form to another page callback. First I had to add an onSubmit javascript event handler to my form to prevent it from submitting.
Now define a javascript function to validate and submit the form using AJAX.
You’ll need a final page callback and menu item to process the AJAX
In the end what we have is a link that opens up a lightbox window that contains a form that is submitted via ajax.
My last thoughts: because the lightmodal functionality loads the URL from the href in the A tag, you might want to create a template file for your page layout that is simplified to prevent your normal page layout from being rendered. For instance, you could create a theme file called, page-MY-URL.tpl.php that contains the following: