Extended and Hidden are there if you know that you need them. Otherwise, leave them alone for your first try. There’s an Action for PHP form handling, but it’s Pro-only. So ironically, as an Express user, you’ll have to be smarter than a pro! (Seriously, forms are not all that mind-bending.)
Let’s start with text fields and text areas (they’re the easiest). After you draw or insert a form field in your page, while it’s still selected, look in the Output tab of the inspector (hover slowly over the tabs to see their labels). There are two fields in that tab that are of critical importance: Name and Value.
###Name
No matter what you want the field to be labeled (for the visitor to see), the Name must be a single word (or several words joined by underscores, like ‘first_name’). It must not begin with a numeral, although it may contain a number or two after the first character. It must be a unique name on that page. If you have two fields named ‘foo’, you will only ever receive the value of the second one.
###Value
This can either be blank, or include a bit of code to “play back” whatever the visitor entered into the field. This second option only comes into play if you are using the Post-Back design pattern for your form handler. (What this means is that you post the form to itself, and a handler script within the form does the processing and error-checking. Why would you do this? Because if there are errors, you can re-load the form with all the previous input, so your visitor doesn’t have to re-type anything they already got “right”.) If you are doing this, then you would enter the same value as the Name field, but formatted like this:
<?php echo( $the_field_name ); ?>
…in the Value field.
###Form Setup Dialog
Set the Method to POST, and the Action leave blank (this will POST the form to itself). Don’t worry about the hidden fields, they’re not needed in this example.
###The Form
Make a new form with two text fields (name and email) and one textarea (message). For extra points, add the PHP code above to each field so it’s set to display what your visitor entered. Add a single submit button at the end, and Name that button submit and give it a Value of Submit.
###The Form Handler
From the main menu, choose Page / HTML Markup, then switch the resulting dialog to Before HTML using the picker in its lower-left corner. Paste in the following (note that this relies on you using exactly the names – in lower-case – as I listed above):
<?php
//extract posted values or set defaults
//adjust this array to include the names of YOUR fields
$fields = array('name','email','message');
$_message = '';
if(isset($_POST['submit'])){
foreach($fields as $field){
$$field = trim(strip_tags($_POST[$field]));
}
}else{
foreach($fields as $field){
$$field = '';
}
}
if(isset($_POST['submit'])){
//basic error checking
//you could have some fields optional by leaving them out
$required = array('name','email','message');
$errors = array();
foreach($required as $field){
if(empty($$field)){
$errors[$field] = ucwords(str_replace('_',' ',$field)) . ' was missing';
}
}
//are there errors?
if( count($errors) > 0 ){
foreach($errors as $e){
$_message .= '<p class="error">' . $e . '</p>';
}
}else{
//no errors, let's e-mail the results
foreach($fields as $field){
$_message .= $field . ': ' . $$field . "n";
}
mail('your-email-here','Subject Line here',$_message,'From: your-email-here','-fyour-email-here');
}
}
?>
###Error Reporting
Finally, somewhere on your page where people can see it, place a new Markup Item to display any error messages. Page / Insert / Markup Item (and paste the following code in the dialog that appears):
<?php echo $_message; ?>
When you okay the dialog, you’ll see a 100px-square box in the center of the screen. Resize this and move it to where you want your errors to appear. Nothing will appear if there are no errors, naturally.
###Whew!
A couple of important notes: 1. This will only work with fields named precisely ‘name’,‘email’,‘message’ using the Name field on the Output tab of the Inspector. 2. You must replace ‘your-email-here’ with your actual e-mail address, and at the end of the script, where you see -fyour-email-here – that’s not a typo. Enter (dash)(f) and then type your e-mail address directly following the f with no space between.
Give this a try on a new blank document, make sure your page has a filename that ends in .php, and upload it to a server. (You won’t be able to test this locally, or even see anything in Preview or Preview in Browser except a page full of code.)
After you get this basic example working, you can move on to more elaborate field types, like picking lists and radio buttons and checkboxes.
Walter
freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options