Hi
I have problem with form which I have created after reading the form API and all the other stuff about forms. I am trying to create one sample form to save user input into a database table. For the sake of understanding the process of form development I have tried to use textfield, date, select, radios and submit button. To insert this data into the database table I am using db_query. Most of the data is going to the database table except the “date”. I have tried many options to resolve this problem but unable to solve it. Following is the table structure;
CREATE TABLE `emp` (
`emp_id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(45) NOT NULL default '',
`designation` varchar(45) NOT NULL default '',
`dob` date NOT NULL default '0000-00-00',
`division` varchar(45) NOT NULL default '',
`gender` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='test table';
Following is the test module for creating form;
//Code Starts here-----------
<?php
// $Id$
/**
* Implementation of hook_menu().
*/
function testform_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'testform',
'title' => t('The Test Form'),
'callback' => 'testform_page',
'access' => TRUE
);
}
return $items;
}
/**
* Called when user goes to example.com/?q=testform
*/
function testform_page() {
$output = t('This page contains our Employee Data Entry Form.');
// Return the HTML generated from the $form data structure.
$output .= drupal_get_form('testform_nameform');
return $output;
}
/**
* Defines a form.
*/
function testform_nameform() {
$form['emp_details']['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#description' => t('Please enter your name.'),
'#required' => TRUE
);
//EMP designation
$form['emp_details']['designation'] = array(
'#title' => t('Designation'),
'#type' => 'textfield',
'#description' => t('Please enter your designation.'),
'#required' => TRUE
);
//EMP dob
$form['emp_details']['dob'] = array(
'#title' => t('Date of Birth'),
'#type' => 'date',
'#description' => t('Please enter your date of birth.'),
'#required' => TRUE,
'#default_value' => array(
'year' => format_date(time(), 'custom', 'Y'),
'month' => format_date(time(), 'custom', 'n'),
'day' => format_date(time(), 'custom', 'j')
)
);
//EMP division
$form['emp_details']['division'] = array(
'#title' => t('Division'),
'#type' => 'select',
'#required' => TRUE,
'#options' => array(t('div-1'), t('div-2'), t('div-3')),
'#description' => t('Please select your division.'),
);
//EMP gender
$form['emp_details']['gender'] = array(
'#title' => t('Gender'),
'#type' => 'radios',
'#required' => TRUE,
'#description' => t('Please select your gender.'),
'#options' => array(
t('Male'),
t('Female')
),
);
//Submit Button
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save')
);
return $form;
}
/**
* Handle post-validation form submission.
*/
function testform_nameform_submit($form_id, $form_values) {
db_query("INSERT INTO {emp} (name, designation, dob, division, gender) VALUES ('%s', '%s', %d, '%s', '%s')", $form_values['name'], $form_values['designation'], $form_values['dob'], $form_values['division'], $form_values['gender']);
drupal_set_message(t('Your form has been saved.'));
}
//Code ends here -----------
?>Please help me in understanding and creating forms in Drupal.
Thanks in advance…
Comments
One more thing
One more thing, whenever I submit this form the default value of date i.e. 0000:00:00 is posted to the database table, irrespective of what I choose in the date control.
please help...
If you have a _validate
If you have a _validate function(hook) then just look at you output for print_r($form-value['dob'])
you can see the output in mm;dd:yyyy , so before saving I think you need to change format to yyyy:dd;mm.
Didn't tried to investigate much into it.
This link would be helpfull
http://drupal.org/node/71962
Hope this will help
Cheers.
Thanks for your
Thanks for your suggestion,
I have tried it and the value returned from
$form_values['dob']is “Array”.Now how do I reference the values of year, month and date?
Following is what i used in validate function;
...
This array holds three elements. First, concatenate them into a nice string:
This will give us a '1973-10-03' string, which is the format databases expect.
Then write it out to the database; use the '%s' placeholder:
Better use 'utf8', not 'latin'. (And all other unnecessary directives there should go.)
Thanks It did worked and
Thanks
It did worked and your suggestions helped me in understanding the things better…
Thank you very much.
Not able to insert date in table
Hai i have used '%s' and changed character set as utf8. But i'm not able to insert date in table. It always insert '0000-00-00'. Please give me a solution
working example
This is working piece of code.
This means in this example format is year-month-day.
To insert date of a node
I am inserting a creation date of node 'project'. This is how I am doing it, but somehow it always inserts '0000-00-00'.
Insert values in table
Hi i'm using this query "db_query("INSERT INTO {emp} (name, designation, dob, division, gender) VALUES ('%s', '%s', %d, '%s', '%s')", $form_values['name'], $form_values['designation'], $form_values['dob'], $form_values['division'], $form_values['gender']);". when i use this division, and gender fields values are entered as '0' or '1' in tables, but i need to insert div-1, div-2, male and female. How to do?
Appending string to the default data
hi I am using multistep form and for that I am displaying 5 date fields , the problem is when the form appears current date is being displayed on the form..
Instead I want "--Year--" "-- month --" "-- date --" strings to display first and then the values beneath them. Plz help this is something urgent I want to cover up
Many Thanks
In advance