By arkjoseph on
Hello, I am trying to build a simple contact form using the form api but I haven't had much success with db_insert. I don't often post question here so I will start by copying my code in:
/*
* 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']);
}
/*
* 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;
}
/*
* Define the form fields
*/
function contact_nameform() {
$form['name'] = array(
'#title' => t('Your Name'),
'#type' => 'textfield',
'#description' => t('Please enter your name.')
);
$form['email'] = array(
'#title' => t('Your Email'),
'#type' => 'textfield',
'#description' => t('Please enter your email.')
);
$form['comments'] = array(
'#title' => t('Comments'),
'#type' => 'textarea',
'#description' => t('Type you comment'),
);
$form['box'] = array(
'#type' => 'markup',
'#prefix' => '<div id="box">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#executes_submit_callback' => false,
'#ajax' => array(
'callback' => 'contact_nameform_submit',
'method' => 'replace',
'wrapper' => 'box'
),
);
return $form;
}
function contact_nameform_submit($form, &$form_state) {
// In most cases, it is recomended that you put this logic in form generation
// rather than the callback. Submit driven forms are an exception, because
// you may not want to return the form at all.
$element = $form['box'];
if ($form_state['values']['name'] == '') {
$element['#markup'] = t('Oops!');
}
elseif ($form_state['values']['email'] == '') {
$element['#markup'] = t('Oops!');
} else {
$element['#markup'] = t('Arigato');
}
return $element;
}
function contact_nameform_validate($form, &$form_state) {
if ($form_state['values']['name'] == '') {
form_set_error('name', t('Name field is required'));
}
if ($form_state['values']['email'] == '') {
form_set_error('email', t('eMail field is required'));
}
}
function contact_nameform_submit_values($form_id, &$form_state) {
$name = $form_state['values']['name'];
$email = $form_state['values']['email'];
$comments = $form_state['values']['comments'];
/*
* MYSQL DB Insertion
*/
$insert = db_insert('custom_contact')
->fields(array(
'name' => $name,
'email' => $email,
'comments' => $comments,
'created' => REQUEST_TIME,
))
->execute();
This submits properly with out ajax so I know it works correctly. I must be missing something that the ajax call back is requesting???
I thank anyone who helps me...this has been plaguing me for 2 days now.
-Joseph
Comments
At first glance I'd say it's
At first glance I'd say it's because you're using '#executes_submit_callback'. From the FAPI documentation:
#executes_submit_callback
Used by: button, image_button, submit
Description: Indicates whether or not button should submit the form.
Values: TRUE or FALSE
To my mind that would mean that your submit button should not submit the form at all, which is what it's doing!
If you're using that flag to stop the form submitting via non-ajax then you shouldn't be, jquery forms will take of preventing the default onclick event of your submit button from being invoked.
update
Thanks _isos. I have removed it.
Okay I found some errors. I don't think I should be calling contact_nameform_submit prior to the validate function. I also don't need conditional within the submit function. That's why I have a separate validate function!
_isos, your correction did not do the trick...can someone please assist me?
thank you
Your function
Your function contact_nameform_submit_values is never called, are you missing a call in your form's submit handler maybe?
_isos, isn't that what the
_isos, isn't that what the following is?
That's the function
That's the function declaration, but where is that function actually called from? I can't see it in the code you've provided, apologies if I've missed it though
I will look into it and post
I will look into it and post back. thank you for the tip
still nothing
Okay, no success as of yet...
Can you test my code please, I am just going bonkers over this simple task. Nothing is getting passed to the database! Is there a firebug plugin available that I could possibly use?
Let me know if the problem is discovered. I took your advise and added the lines under // NEW LINE ADDED . Let me know if im doing things correct.
-Joe
You're nearly there, in your
You're nearly there, in your contact_name_submit function you've got 'return $element;' before you commit the record to the database. Move this code:
to the bottom of contact_name_submit, after you've written the record to the database. It should work fine then
great!
It works! Updated code...
now I seem to have a strange issue of the date getting input twice when i only clicked submit once. Could it be due to my validation?
sorry, not the date, the
sorry, not the date, the data...
I'm not 100% on this but I
I'm not 100% on this but I think the submit handler would automatically be called for an ajax callback anyway, so if your ajax callback is also the submit function it'll get run twice.
If you separate the code out it should work
Then change you ajax callback for $form['submit'] in contact_nameform() to 'contact_nameform_ajax_callback' and you should be good to go!
I'm not 100% on this but I
I'm not 100% on this but I think the submit handler would automatically be called for an ajax callback anyway, so if your ajax callback is also the submit function it'll get run twice.
If you separate the code out it should work
Then change you ajax callback for $form['submit'] in contact_nameform() to 'contact_nameform_ajax_callback' and you should be good to go!
EDIT: I've updated it to be a bit cleaner
THANK YOU!
Works like a charm. I am learning form API on my own until now.
thank you!