0

In the example below I want to take elements from the form and include them as part of the URL that I am submitting the form (using #action) so the submitted URL would look like http://example.com/name_storage/John/Lewis/123412341234.

    <?php


function my_module_menu() {
  $items[‘testform’] = array(
    'title' => 'First Last Name',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_module_form'),
    'access callback' => TRUE,
    );
  return $items;
}

function my_module_form($form,&$form_submit) {
  $form[‘first_name’] = array(
    '#title' => t(The first name of the person),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form[‘last_name’] = array(
    '#title' => t('The first name of the person'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['passcode'] = array(
    '#title' => t('Passcode for this domain.'),
    '#type' => 'textfield',
    '#value' => '123412341234',
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#value' => ‘Submit,
    '#type' => 'submit',
    );
  $form['#action'] = url('http://example.com/name_storage/' . $form['first_name']['#value'] . '/'. $form['first_name']['#value'] . '/' . $form['passcode']['#value']);
  return $form;
}
?>
1

In FAPI, #action changes the form's action parameter, which, you should not override if you want to make Drupal handle the submission.

In your case, you need to define a custom submit handler and redirect the user in that function. You could do something similar by changing the form's method to GET, but it is not possible to define a complex path like the one you want.

Remove this line:

$form['#action'] = url('http://example.com/name_storage/' . $form['first_name']['#value'] . '/'. $form['first_name']['#value'] . '/' . $form['passcode']['#value']);

Then, add a new submit handler like this:

function my_module_form_submit($form,&$form_state) {
  $values = $form_state['values']['first_name'] . '/'. $form_state['values']['last_name'] . '/' . $form_state['values']['passcode'];
  $form_state['redirect'] = 'name_storage/' . $values;
}

To set the redirect parameter to an external URL, you can do this:

$values = 'http://example.com/name_storage/';
$values .= $form_state['values']['first_name'] . '/'. $form_state['values']['last_name'] . '/' . $form_state['values']['passcode'];
$form_state['redirect'] = array(
  $values,
  array(
    'external' => TRUE,
  ),
);
  • Great, now how do I get it to submit to an external domain (in this it's going to example.com instead of the drupal site itself)? – John Lewis Jun 4 '14 at 17:31
  • 1
    Just change the $form_state['redirect'] to whatever URL you need. The example in the answer is for internal links but you still can put any URL there. – AyeshK Jun 4 '14 at 17:34
  • Oddly, if I put example.com in front, I get an error where it appears that apache (MAMP) is url decoding the url encoded string I used for the values... I see the URL in the redirected URL as encoded, but it's treating the encoded %252F is getting decoded as /'s and I'm not sure why (obviously results in error). Only when I add the domain to the front, however. – John Lewis Jun 4 '14 at 18:16
  • 1
    @JohnLewis See the updated answer. – AyeshK Jun 4 '14 at 22:14

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.