Field for phone number on contact form

jakeg - September 11, 2006 - 08:09

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.

Correct me if I'm wrong, but I don't think there's any other way to do this - I can't use hook_nodeapi as the contact form is not a node. So the only hook I seem to be able to use to gain access to the form is hook_form_alter.

Just added a new module for

thierry_gd - October 8, 2006 - 15:53

Just added a new module for creation / validation of phone field via CCK
check it out at : http://drupal.org/project/phone

It 's a beginning, can still be improve

------------
Thierry Guégan
Paris + Vancouver

Another approach using your own _submit function

alexis - December 1, 2006 - 21:41

Hey, your post gave me the idea to add a "subscribe to newsletter" checkbox in my site-wide contact form.

I added my own _submit function and put it together as a small module that I called newsletter checkbox.

Regards!

Alexis Bellido
Ventanazul: the first bilingual forum for web developers.

Try the WebForm module

NancyDru - March 2, 2007 - 01:19

Nancy W.
now running 5 sites on Drupal so far
Drupal Cookbook (for New Drupallers)
Adding Hidden Site Design Notes

Will Webform work with Gotcha Module

Thomasr976 - June 20, 2008 - 14:39

I have developed a web form as suggested and enabled capcha. But now that I've abandoned the Site wide contact form I've lost Gotcha. Is there anyway to tie it in to the new webform?

In the future

NancyDru - June 20, 2008 - 15:24

This is a planned update to Gotcha, but is awaiting the rewrite of Spam.

NancyDru (formerly Nancy W. until I got married to Drupal)

another way

akno - January 9, 2008 - 23:19

I added these lines into contact.module:

in the section "Site-wide contact page", added a phone field under the mail field so that it looks like:

$form['mail'] = array('#type' => 'textfield',
'#title' => t('Your e-mail address'),
'#maxlength' => 255,
'#default_value' => $user->uid ? $user->mail : '',
'#required' => TRUE,
);
$form['phone'] = array('#type' => 'textfield',
'#title' => t('Phone'),
'#maxlength' => 20,
'#required' => TRUE,
);

and then to include it into the autogenerated e-mail body, I added one line to "prepare the body" section as:

$body = implode("\n\n", $message);
$body = $body . "\n\nPhone: " . $form_values['phone'];

Hmm...

NancyDru - January 10, 2008 - 01:00

Probably 75% of us have done that, but as soon as the next release comes along, it goes away again. I would recommend the Contact Forms module much more than a core hack.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

How does 'contect forms' help this issue?

JDSaward - March 3, 2008 - 04:26

I agree that hacking core is not the way to go. I cannot though - yet- see how the Contact Forms module aids in adding a field for the phone number to the contact form. Can you explain?

John

Well...

NancyDru - March 3, 2008 - 12:37

It doesn't, by itself, add the field; you must still create a form. But it then allows that form to be used in place of the standard contact form. When I did this, I used the Webform module.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Thanks

JDSaward - March 4, 2008 - 07:19

Yes; that makes sense. Thanks.

hacking the cocntact form in v6

amax - May 25, 2008 - 16:43

Dear another way,

I have installed drupal version 6 and used you hack above, only it seems that i had to modify the contact.pages.inc file to add the following:

$form['phone'] = array('#type' => 'textfield',
'#title' => t('Phone'),
'#maxlength' => 20,
'#required' => TRUE,
);

as for the $body = $body . "\n\nPhone: " . $form_values['phone']; part, i have no idea where thtis goes in the new version. Can you please help me with this

while your at it...

marlowx - June 12, 2008 - 13:27

what about Name? first and last?

and what about doing this so people can easily extend it? perhaps day phone and night phone for example? or work and home phone?

or best time to call?

you get the idea...

thank guys!

 
 

Drupal is a registered trademark of Dries Buytaert.