What I want to do: On the Home page, I have an input field in which a user can insert their invite code, the invite code gets validated via ajax and if correct, popups the registration form. Its important that the user cannot get access to the registration form without the correct invite code. How can I bullet proof this concept into my Drupal7 install? I am using Regcode module for registration purpose.

The problem I have is that if the user knows the address to the registration form, they can merely enter that into the browser's address bar and register without having to gain an invite code.

One solution I thought was that the invite code is needed on the actual registration form as well, so if they do access the form via the browser's address bar, they will still need to input the invite code. However, if they already did place the invite code in the input field (on the Home page), it would automatically populate the invite code field in the registration form.

What problem I am facing: Im not sure how to go about transferring the invite code (which the user inserted) into the registration form that pops up upon validation. The form(single field) on the front page is written by myself inside a block.

Here i am confused how to take the form value to the registration form and how to get the value of field and after getting how to set the value as default in the regcode text field.

 <form method="GET" action="?????"></form>

the modulea's part which alter the registration form is as follows

 function regcode_form_user_register_form_alter(&$form, &$form_state) {
  $code_optional = variable_get('regcode_optional', FALSE);
  $account = $form['#user'];

  $form['regcode']['regcode_code'] = array(
    '#type' => 'textfield',
    '#title' => variable_get('regcode_field_title', t('Registration Code')),
    '#description' => variable_get('regcode_field_description', t('Please enter your registration code.')),

    '#default_value' => ???????????,

    '#required' => !($code_optional || user_access('administer users')),
    '#element_validate' => array('regcode_code_element_validate'),
  // Capture the code from the url and inject it into the registration form
  if (isset($_GET['regcode'])) {
    $form['regcode']['regcode_code']['#value']       = check_plain($_GET['regcode']);
    $form['regcode']['regcode_code']['#description'] = NULL;
    $form['regcode']['regcode_code']['#disabled']    = TRUE;

i Hope, its understandable now

  • What kind of form it is? Do you want to manage setting default value for each user session? What do yo mean for "got from another page(form)"? Jun 23, 2011 at 8:55

2 Answers 2


Drupal doesn't store the form state between forms, so you will need to do that yourself.

In this case I would use the session variable since you want to save it per user, but not permanently.

To do this you will need to add a custom submit handler to the form if you form is not created by you, this can be done with hook_form_alter

Then you can save the value you want to save in the session variable something like

 $_SESSION['my_value'] = $value;

should do the trick, then you can set the value in the form, either directly if you control it or via form alter.

That the form value is gotten from or is to be set to a field doesn't change things. The form field structure is a little more verbose, but the way to do it is pretty much the same.

  • i saved the value in _SESSION by a php program and to make it default value of regcode field(register forms) i added the following line. '#default_value' => variable_get('regcode_field_default_value', $_SESSION['key']), but it gives the error as under. Error message Notice: Undefined variable: _SESSION in regcode_form_user_register_form_alter() (line 148 of C:\wamp\www\may18\sites\all\modules\regcode\regcode.module). Please help me how to make it in a correct way. Jul 2, 2011 at 10:34
  • @Mujtaba Mir Update your question with exact code.
    – googletorp
    Jul 2, 2011 at 10:40

Maybe try looking at the Contextual Administration module. I have personally not used it but it was described to me in way that it seems pertinent to the situation you are describing (I may be wrong though)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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