By arkjoseph on
I have gotten the ajax submission down, now I need the form to fade out after submit with perhaps a message, "thank you for submitting".
<?php
/**
* FORM API
* Basic Contact form that inserts data into db and sends an email to both parties
*/
/*
* Implements hook_menu()
*/
function contact_menu() {
$items['contact'] = array(
'title' => 'View the sample form',
'page callback' => 'drupal_get_form',
'page arguments' => array('contact_nameform'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/*
* Implements hook_theme
*/
function contact_theme() {
return array(
'contact_nameform' => array(
'render element' => 'form',
'template' => 'contact_nameform',
),
);
}
/*
* Assign the elements of the form to variables
*/
function template_preprocess_contact_nameform(&$variables) {
$variables['contact_nameform'] = array();
$hidden = array();
//Provides variables named after form keys
foreach (element_children($variables['form']) as $key) {
$type = $variables['form'][$key]['#type'];
if ($type == 'hidden' || $type == 'token') {
$hidden[] = drupal_render($variables['form'][$key]);
}
else {
$variables['contact_nameform'][$key] = drupal_render($variables['form'][$key]);
}
}
// Hidden form elements have no value to the theme
$variables['contact_nameform']['hidden'] = implode($hidden);
// Collect all form elements
$variables['contact_nameform_form'] = implode($variables['contact_nameform']);
}
/*
* Define the form fields
*/
function contact_nameform() {
// Assign form ID
$form['#id'] = 'contact';
$form['#contact'] = $contact;
$form_state['contact'] = $contact;
// Fields
$form['name'] = array(
'#title' => t('Your Name'),
'#type' => 'textfield',
'#description' => t('Please enter your name.'),
'#required' => TRUE
);
$form['email'] = array(
'#title' => t('Your Email'),
'#type' => 'textfield',
'#description' => t('Please enter your email.'),
'#required' => TRUE
);
$form['comments'] = array(
'#title' => t('Comments'),
'#type' => 'textarea',
'#maxlength' => 500,
'#description' => t('Type you comment'),
);
$form['submit'] = array(
'#type' => 'submit',
'#ajax' => array(
'method' => 'replaceAll',
'callback' => 'contact_nameform_ajax_callback',
'wrapper' => 'box',
'effect' => 'fade',
'#value' => t('Submit'),
),
);
return $form;
}
/*
* Form Builder; display confirmation of submission
*/
function contact_nameform_confirm($form, &$form_state) {
}
/*
* Form submit function and email
*/
function contact_nameform_submit($form, &$form_state) {
// POST Info
$name = $form_state['values']['name'];
$email = $form_state['values']['email'];
$comments = $form_state['values']['comments'];
$params['form'] = $form_state;
// New db_insert
db_insert('custom_contact')
->fields(array(
'name' => $name,
'email' => $email,
'comments' => $comments,
'created' => REQUEST_TIME,
))
->execute();
// Send email to admin
$admin ="doo@doo.com";
$from = "foo@foo.com";
$result = drupal_mail('contact', 'notify', $admin, language_default(), $params, $from, $send = TRUE);
}
/*
* hook_mail().
*/
function contact_mail($key, &$message, $params) {
switch ($key) {
case 'notify':
$message['subject'] = t('Asakos Website Contact Form - '. $params['form']['values']['name']);
$message['body'][] = t('New information has been recorded. Contact information is below:')."\n".
t('Name: '. $params['form']['values']['name']) ."\n".
t('E-mail: '. $params['form']['values']['email']) ."\n".
t('Comments: '. $params['form']['values']['comments']) ."\n".
// Escape unintentional html inside of body
$message['body'][] = check_plain($params['message']);
break;
}
}
function contact_nameform_ajax_callback($form, &$form_state) {
$element = $form['box'];
// if email get submitted succesfully
$element['#markup'] = "Clicked submit with successful validation";
// No it has not validated
return $element;
}
?>
From my research so far, I need to add a form element like so:
$form['confirm_message'] = array(
'#type' => 'value',
'#value' => $confirm,
'#description' => t('Thank you for your interest!'),
);
I then need to return the value of this element inside of the ajax callback.
Am I on the right track?
Thanks