How to make contact form in Drupal

We can make contact form in drupal using the following codes and we can redirect the value to a URL  .

<?php
function contact_us_form() {
$form[‘name’] = array(
‘#type’ => ‘textfield’,
‘#title’ => ”,
‘#size’ => ’50’,
‘#attributes’ =>array(‘placeholder’ => t(‘Name’))
);
$form[’email’] = array(
‘#type’ => ‘textfield’,
‘#title’ => ”,
‘#size’ => ’50’,
‘#attributes’ =>array(‘placeholder’ => t(‘Email’))
);
$form[‘#action’] =  ” ;
$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#class’ => ‘button’,
‘#value’ => t(‘Contact US’),
);
return $form;
}

function contact_us_form_validate($form, &$form_state) {
$val_email = $form_state[‘values’][’email’];
if (!valid_email_address($val_email)) {
form_set_error(’email’,’Invalid Email’);
}
}
return drupal_render(drupal_get_form(‘contact_us_form’));

?>

In  $form[‘#action’] =  ” ; You can set here the URL where you want to redirect after submitting the form .

This code can be used in the article or block section of drupal but you need to select PHP as filter type from drop down .

Leave a Reply

Your email address will not be published. Required fields are marked *