1

I wanted to know how to get the username for whoever submits a form so I can pass it to db_insert along with form data.

Sorry, I'm new to Drupal and php. I'm just try to learn.

Yes, it is custom code. Here is the form. I have based it on tutorials I've found so far.

function input_simple_form($form, &$form_submit) {

$form['color'] = array(
    '#title' => t('color:'),
    '#type' => 'textfield',
    '#required' => TRUE,
            '#description' => t('What is your favorite color?'),
    '#prefix' => '<div class="container-inline">', 
    '#suffix' => '</div><br>',
    );
$form['TV_Show'] = array(
    '#title' => t('TV_Show:'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#description' => t('What is favorite TV show?'),
    '#prefix' => '<div class="container-inline">', 
    '#suffix' => '</div><br>',
    );

$form['input'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#submit' => array('input_simple_form_submit'),
    );

return $form;   
}

Then I send the data to database:

function input_simple_form_submit($form, &$form_state) {

// Display a message upon successful submission.
drupal_set_message(t("Added @color @TV_Show", array('@color' => $form_state['values']['color'], '@Name' => $form_state['values']['TV_Show'])));
$id = db_insert("TestForm")
->fields(array(
    'color' => $form_state['values']['color'],      
    'TV_Show' => $form_state['values']['TV_Show'],
    'SubmitBy' => $user->name,
    ))
->execute();
}

I'm not sure how to pass $user to the submit function. Hope that helps.

3
  • Why would you need that? It will be the same as username of currently logged in user, and that's pretty readily available, no need to pass it in form.
    – Mołot
    May 29 '14 at 10:46
  • This question appears to be off-topic because it is about code you wrote, for which you want to write better code, or you want to understand why it doesn't work, but you didn't show the code you are using, or the part of the code that is relevant for the question. See Help Center.
    – Mołot
    May 29 '14 at 10:47
  • Note that to get the proper way to get the username if the format_username() function (see api.drupal.org/api/drupal/includes!common.inc/function/…) Jul 4 '14 at 19:37
2

You don't pass $user to the submit function.

Instead, you add a declaration of the global $user object to the start of the submit function to bring it into scope. Like this:

function input_simple_form_submit($form, &$form_state) {
  global $user;

  // Display a message upon successful submission.
  drupal_set_message(t("Added @color @TV_Show", array(
    '@color' => $form_state['values']['color'],
    '@Name' => $form_state['values']['TV_Show'],
  )));
  $id = db_insert("TestForm")
    ->fields(array(
      'color' => $form_state['values']['color'],        
      'TV_Show' => $form_state['values']['TV_Show'],
      'SubmitBy' => $user->name,
    ))
    ->execute();
}
1

You can always get the user object using global keyword:

global $user;
$uid = $user->uid;
$username = $user->name;
0

In your custom code, you need to use

global $user;
$username = $user->name;

Regards,

K.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.