I'm unable to save the details into database after the form is submitted in Drupal 7.
Below is the code for my custom form to save the details to DB,
function form_module_menu() {
$items = array();
$items['form_module/form'] = array(
'title' => t('Registration form'),
'page callback' => 'form_module_form',
'access arguments' => array('access content'),
'description' => t('Registration Form'),
'type' => MENU_CALLBACK,
);
return $items;
}
function form_module_form() {
// This form calls the form builder function via the
// drupal_get_form() function which takes the name of this form builder
// function as an argument. It returns the results to display the form.
return drupal_get_form('form_module_my_form');
}
function form_module_my_form($form_state) {
$form = array();
// This is the first form element. It's a textfield with a label, "fName"
$form['fName'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE,
'#size' => 60,
'#maxlength' => 64,
);
// This is the lastname form element. It's a textfield with a label, "lName"
$form['lName'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
);
// This is the email address form element. It's a textfield with a label, "email"
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#required' => TRUE, //must fill this if set to TRUE
'#rules' => array(
),
'#filters' => array('trim', 'uppercase')
);
// This is the password and confirm password form element. It's a textfield with a label, "cpwd"
$form['cpwd'] = array(
'#type' => 'password_confirm',
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['clear'] = array(
'#type' => 'submit',
'#value' => 'Reset form',
'#validate' => array('my_module_my_form_clear'),
);
//print l('return to main home page', '<front>');
return $form;
}
//Rebuild the form
function my_module_my_form_clear($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}
function form_module_email_validate($element, &$form_state, $form) {
//email validation not working how to validate email
$mail = $form_state['values']['email'];
if (strpos($mail,'@') !== true) {
form_error($element, t('Please enter a valid email address.'));
}
else{
form_error($element, t('Please enter a valid email address.'));
}
}
function form_module_my_form_submit($form, &$form_state) {
drupal_set_message($form_state['values']['fName'].$form_state['values']['lName'].$form_state['values']['email']);
$sql = "INSERT INTO {customers} (first_name,last_name,email_addr,password) VALUES('%s', '%s', '%s' , '%s')";
db_query($sql,
array('%s' => $form_state['values']['fName']),
array('%s' => $form_state['values']['lName']),
array('%s' => $form_state['values']['email']),
array('%s' => $form_state['values']['cpwd'])
);
drupal_set_message(t('Thanks for registering with us. You will receive an email shortly to activate your account.'));
//after successfully saved how to send mail to user with his login details
}
The submit handler is called and the values are displayed.but in database all rows are filled with '%s'. I new to working in Drupal. How to store passwords and how to save the data and send an email to the user registering..