Take the 2-minute tour ×

I've recently changed my form to use Ajax to submit the data. My form accepts dates from the user in (mm-dd-yyyy) format, and prior to this change my hook_submit seen below as would convert it to a (yyyy-mm-dd) format on my behalf before it sent it to the DB. After the change, it skips that step and sends the date value to the DB as is.

I don't mind finding or writing my own function that will do the conversion, but I'd first like to understand the nature of this before I do. Perhaps I'll learn something that will help me later on.

Any help would be appreciated.

My question is, why is my date value converted with $form_data and not with $form_state?

Using Drupal 7.

Modules: Date API, CTools,

//Original submit handler
//Format sent to DB: yyyy-mm-dd <--This is what I want for the Ajax version
function fsa_roster_form_submit($form_id, &$form_data){
$dcc_table = 'dcc_'.$form_data['values']['sid'];
$insertDaycare = db_insert($dcc_table)
        'entered' => $form_data['values']['date_entered'],
        'exited' => $form_data['values']['date_exited'],

//Ajaxed submit handler:
//Format sent to DB: mm-dd-yyyy
function fsa_roster_form_ajax_submit($form, $form_state) {
    $dcc_table = 'dcc_'.$form_state['input']['sid'];
    $insertRoster = db_insert($dcc_table)
        'entered' => $form_state['input']['date_entered'],
        'exited' => $form_state['input']['date_exited'],
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Entry'),
    '#prefix' => '<div id="modalSubmit">',
    '#ajax' => array(//Remove this from your imagination for original submit handler
        'callback' => 'fsa_daycare_roster_form_ajax_submit',
        'wrapper' => 'msgBox',
        'method' => 'replace',
        'effect' => 'fade',
    '#suffix' => '</div><script>addValidationEvents();</script>'
share|improve this question
instead of coding that, have you tried changing the "Date format" in drupal configuration? so all dates is outputed in that format.. –  Phoenix Jul 26 '12 at 5:49

1 Answer 1

up vote 1 down vote accepted

The reason is that you are using $form_state['input'] in the AJAX submit callback, and $form_state['values'] in the regular submit callback. The input array always contains the form data before it is processed and validated, while the values array contains the values after processing and validation have been applied.

So to achieve the same effect in your AJAX submit callback, use the following, just like you did in your regular submit callback:

function fsa_roster_form_ajax_submit($form, $form_state) {
  $dcc_table = 'dcc_'.$form_state['values']['sid'];
  $insertRoster = db_insert($dcc_table)
    'entered' => $form_state['values']['date_entered'],
    'exited' => $form_state['values']['date_exited'],

Also, for what it's worth, your regular submit callback and the AJAX submit callback both accept the same parameters: $form and $form_state. Though you called them by different variable names in the two functions, they are actually (more or less) identical.

share|improve this answer
Thank you! Your answer was very helpful, and I gained a better understanding of $form_state input and values. –  SFox Aug 14 '12 at 1:19

Your Answer


