|< Day Day Up >|
Walk your users through the form-filling process .
Collecting information with online forms is an interactive process. We have discussed how to collect form data [Hack #2] and how to drive forms [Hack #75] . Now, let's use what we know to program an interactive, online form-filling session, such as the one in Figure 6-6 (visit http://www.pdfhacks.com/form_session/ to see this example and download PHP source code).
Figure 6-6. A PDF form's dynamic state
6.5.1 Set the Stage
To submit data to your server, the PDF form must be displayed inside a web browser. I recommend displaying it inside an HTML frameset. This enables you to bracket the form with (HTML) instructions and a hyperlinked escape route, so the user won't feel abandoned or trapped. It also adjusts the user to the idea that this isn't any old PDF. Most people experience PDF as something to download and print out. Not only will this look different, but also the frameset conceals the PDF's URL and prevents reflexive downloading.
Here is the HTML frameset code used in our example at http://www.pdfhacks.com/form_session/. Note that it uses a PDF+FDF URL to reference the form in order to prevent the PDF from breaking out of the frameset [Hack #75] .
<?php // This is part of form_session // visit www.pdfhacks.com/form_session/ // $our_dir= 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); // The PDF+FDF URL notation respects frames // and triggers the browser's 'PDF' association // instead of its 'FDF' association (some browsers // don't have an FDF association). // $form_frame_url= '"'. $our_dir.'/form_session.pdf#FDF='. $our_dir.'/update_state.php?reset=1"'; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html> <head> <title>PDF Form Filling Session Demo</title> </head> <frameset cols="*,200"> <frame src=<?php echo $form_frame_url ?> name="form" scrolling="no" marginwidth=0 marginheight=0 frameborder=1> <frame src="sidebar_session.html" name="sidebar" scrolling="auto" marginwidth=5 marginheight=5 frameborder=1> </frameset> </html>
6.5.2 Create Your Interactive PDF Form
Here are some ideas for your interactive PDF form design. Keep in mind that a form can have any number of PDF fields, and you can hide fields from the user or set them as read-only at any time. Our forge_fdf script [Hack #77] enables you to set these flags as needed. Just add the field's name to the $fields_hidden or $fields_readonly arrays.
We employ some of these techniques in our online example at http://www.pdfhacks.com/form_session/.
6.5.3 Beginning, Middle, End
A form-filling session has a beginning, a middle, and an end. The middle is the hard part because that is where your form logic is. The tricky parts are the beginning and the end. Here they are, in order:
6.5.4 Running the Hack
Visit http://www.pdfhacks.com/form_session/ to see a live example of this model. Download form_session-1.1.zip from this page to examine the PHP scripts and PDF forms. IndigoPerl [Hack #74] users can unpack form_session-1.1.zip into C:\indigoperl\apache\htdocs\pdf_hacks\form_session-1.1\ and then run the example locally by pointing their browsers at http://localhost/pdf_hacks/form_session-1.1/start.html .
|< Day Day Up >|