By arkjoseph on
I am stepping over my own feet here. I have created an ajax form submission that stores to a db. Now I need to get an email notification sent to the site admin.
Here is the code producing an ajax error each time I submit the form:
<?php
/**
* FORM API
*/
/*
* 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['info'] = array(
'#type' => 'value',
'#value' => ($contact),
);
$form['box'] = array(
'#type' => 'markup',
'#prefix' => '<div id="box">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#ajax' => array(
'callback' => 'contact_nameform_ajax_callback',
'method' => 'replace',
'wrapper' => 'box'
),
'#value' => t('test form'),
);
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) {
$mailingList ="foo@foo.com";
$from = "noreply@foo.com";
drupal_mail(
'contact_nameform',
'send',
$key,
$mailingList,
language_default(),
$form_state['values'],
variable_get('site_mail', NULL),
$from,
TRUE
);
// POST Info
$name = $form_state['values']['name'];
$email = $form_state['values']['email'];
$comments = $form_state['values']['comments'];
// New db_insert
db_insert('custom_contact')
->fields(array(
'name' => $name,
'email' => $email,
'comments' => $comments,
'created' => REQUEST_TIME,
))
->execute();
}
/*
* hook_mail().
*/
function contact_nameform_mail($key, &$message, $params) {
switch ($key) {
case 'send':
$contact = $params['contact'];
$subject = variable_get('name');
$body = variable_get('message');
$message['to'] = $account->mail;
$message['subject'] = $subject;
$message['body'][] = $body;
break;
}
}
function contact_nameform_ajax_callback(&$form, &$form_state) {
$element = $form['box'];
$element['#markup'] = "Clicked submit ({$form_state['values']['op']}): " . date('c');
return $element;
}
?>
drupal_mail i believe might be producing the error but I am not completely sure.
If some one can help me just get a blank email sent, I can figure out the rest.
Here is the ajax error on submission:
An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /system/ajax
StatusText: OK
ResponseText:
Fatal error: Cannot use object of type stdClass as array in /Users/JosephPGarrido/Sites/ai/sites/all/modules/custom/contact/contact.module on line 152
Comments
drupal_mail() takes a maximum
drupal_mail() takes a maximum of 7 parameters. You have left in $key (which is undefined) and means that all the rest of the parameters are incorrect. This is the root of your problem.
Contact me to contract me for D7 -> D10/11 migrations.
no more error
Thanks Jay. I got rid of $key, no more error : )
For my setup, what do you recommend goes inside of drupal_mail() for me to successfully send email?
currently it is:
-Joseph
Your call to drupal_mail is
Your call to drupal_mail is still incorrect. You have set the language to 'noreply@foo.com', the params to 'language_default()' and the from address as 'TRUE'.
Read the documentation on drupal_mail(). Until you get your call right, it's not going to work.
Contact me to contract me for D7 -> D10/11 migrations.
Yeah, your arguments are in
Yeah, your arguments are in the wrong order. I think you're looking for something like this:
Oh, also, if you haven't changed it already, your function contact_nameform_mail() should be named contact_mail(). Personally I'd use a more descriptive key than 'send' (like maybe 'nameform'?), but that's more a matter of taste, I think.
++Andy
Generally I agree with more
Generally I agree with more descriptive keys in order to prevent namespace collisions, but with hook_mail, drupal_mail() has to specify the module in which the hook_mail() implementation resides, so you don't have to worry about namespace collisions.
Contact me to contract me for D7 -> D10/11 migrations.
Wonderful
Thanks Guys, I got it working now. I decided to check my spam folder finally :\
Once I finish this portion up, I need form preview or confirm.
Does anyone know where I can look for form preview and/or confirmation of ajax forms? I found form_confirm() but not sure if this can be applied to ajax submissions. Any suggestions on how to implement this feature? Should I start a new post?
You can create your
You can create your confirmation as a form element, and return it from your ajax callback. You will have to ajaxify the returned data as well however, in order to be able to submit it.
Contact me to contract me for D7 -> D10/11 migrations.
ok
Thanks Jay. How about if I simply want to get rid of the form on submit if validation = TRUE . I don't like that the form still is there after I submit it. I would like to see it fade(). Does the api have this feature built in?
Im reading about the replace method but I am not sure on how to implement it and if it is even what I need.
Further research suggests that I implement the _validate() function manually...anyone else have success accomplishing this?
Exactly...
Exactly, the key gets combined with module for the purposes of calling hook_mail_alter, so any mail altering modules would be looking out for 'contact_send' in this instance. If your 'contact' module only sends one mail, then 'send' is a perfectly adequate key.
(This post in response to Jay's comment about mail keys above. Didn't realise we'd run up against the comment nesting limit!)
++Andy
Ajax
and your ajax callback is getting called?