Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

i want to add few html elements call first name and last name,company name in top of user/register page and save that data when user click create account button..,how can i achieve that,i add html elements in block as normal html tags like but dont't kow how to get data...thanks

share|improve this question

2 Answers

up vote 5 down vote accepted

Does it have to be in a block?

If you want to add fields to the registration form and do something with the values when they submit, you really have to add them to the registration form itself, not in a block somewhere nearby.

To do this you can create a custom module that implements a form alter.

For example:

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function MODULENAME_form_user_register_form_alter(&$form, &$form_state) {
  // Add your fields.
  $form['firstname'] = array(
    '#type' => 'textfield',
    '#title' => t('First name'),
    '#description' => t("This is help text that will display with this field. you can leave it off if you don't need it."),
    '#required' => TRUE, // Leave this line out if you don't want a required field.
    '#weight' => -10, // This controls the field order. Smaller number is higher up the form.
  );
  $form['lastname'] = array(
    '#type' => 'textfield',
    '#title' => t('Last name'),
  );
  $form['company'] = array(
    '#type' => 'textfield',
    '#title' => t('Company'),
  );

  // Add your own submit handler to save the values.
  $form['#submit'][] = 'MODULENAME_custom_submit_handler';
}

/**
 * Custom submit handler for user register form.
 */
function MODULENAME_custom_submit_handler($form, &$form_state) {
  // Do whatever you need to save the values here.
  // Submitted values are in $form_state['values'] like this.
  $fname = $form_state['values']['first_name'];
  $lname = $form_state['values']['last_name'];
  $company = $form_state['values']['company'];
}

Now having said all that, is there some reason you want to do this custom? It is possible to use a module that lets you add fields to the register form, like the profile2 module. That module lets you add profiles (essentially a group of fields, similar to a node) to users, which can also be displayed on the registration form for the user to fill out at that point.

The module handles saving the data for you, along with a lot of other features, like views support.

share|improve this answer

Just go to the people category on your configuration page , click on account settings and select the the manage fields tab, and create your new fields. You don't need html elements to do that

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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