I wanted a field on my site-wide contact form for the user's phone number. Here's the code I added to my module in hook_form_alter() to add it:
// Add phone number to contact mail page
if ($form_id == 'contact_mail_page') {
$form['phone'] = array(
'#type' => 'textfield',
'#title' => t('Your phone number'),
'#maxlength' => 255,
'#default_value' => $edit['phone'],
'#required' => false,
);
$form['name']['#weight'] = 0;
$form['mail']['#weight'] = 2;
$form['phone']['#weight'] = 4;
$form['subject']['#weight'] = 6;
$form['message']['#weight'] = 8;
$form['copy']['#weight'] = 9;
$form['submit']['#weight'] = 10;
// Add the phone number to the email
if ($_POST['edit']['phone']) {
$_POST['edit']['message'] = t('Phone number: ') . $_POST['edit']['phone'] . "\n\n" . $_POST['edit']['message'];
};
}
You can see my implementation on my yearbook website.
What this does is adds a field for phone number and on submission of the form, it puts the phone number into the top of the body field.
The only problem is that if the user's form doesn't validate (e.g. they leave out the subject field) then when the form comes back to them to complete the phone number will already have been added to the subject field, as well as being in the phone field. Hence when they submit again, you will have the phone number twice.