I have a form where the user types in a taxonomy term (which is autocompleted). When the user submits the form, I want the taxonomy term appended to the url.

How do I write the code so that the taxonomy term that is entered is included in the redirect url?

function find_a_lawyer() {
$form['taxonomy']['tags'][$vocabulary->vid] = array(
'#type' => 'textfield',
'#default_value' => $typed_string,
'#size' => 30,
'#maxlength' => 100,
'#autocomplete_path' => 'taxonomy/autocomplete/5'. $vocabulary->vid,
'#required' => $vocabulary->required,
'#title' => $vocabulary->name,
'#description' => t('Enter the State or Province where you would like to find a representative").')
);

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Search'),
);
$form['#redirect'] = array('node/FindLawyer/'.$arg);


 $output = drupal_get_form('find_a_lawyer', $form);
 return $output;

Comments

pwolanin’s picture

Where's your submit function? How is $vocabulary->vid getting in there? Are you doing this for several vocabs? Try something like this:

function find_a_lawyer($vocabulary) {

$form['taxonomy']['tags'][$vocabulary->vid] = array(
'#type' => 'textfield',
'#default_value' => '',
'#size' => 30,
'#maxlength' => 100,
'#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,
'#required' => $vocabulary->required,
'#title' => $vocabulary->name,
'#description' => t('Enter the State or Province where you would like to find a representative").')
);

$form['vocab'] = array(
'#type' => 'value',
'#value' => $vocabulary,
);

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Search'),
);

$output = drupal_get_form('find_a_lawyer', $form);
return $output;
}

function find_a_lawyer_submit ($form_id, $form_values) {
   $vocabulary = $form_values['vocab'];
  $arg = $form_values[$vocabulary->vid];
  return 'node/FindLawyer/'.$arg;  // your redirect path here.  or use drupal_goto().
}

---
Work: BioRAFT

hubris’s picture

Hi Pwolanin,

I had tried many variations on the code... the one I pasted (without the submit) was just the latest desperate variation...

I tried your code, but it wasn't working.. $arg = $form_values[$vocabulary->vid]; still returns nothing...

The Forms Quickstart guide says that that array tree element is the name/key that the value will be under... but neither your code or mine was ...properly... accessing the value under the correct key.

So I tried something a little off the wall:

function find_a_lawyer() {
 $form['tags'] = array(     // ['taxonomy']['tags'][$vocabulary->vid] = array(
'#type' => 'textfield', 
'#default_value' => $typed_string,
'#size' => 30, 
'#maxlength' => 100, 
'#autocomplete_path' => 'taxonomy/autocomplete/5', 
'#required' => $vocabulary->required, 
'#title' => $vocabulary->name, 
'#description' => t('Enter the State or Province where you would like to find an ELA representative.')
);

 $form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Search')
);

$form['vocab'] = array(
'#type' => 'value',
'#value' => 'taxonomy/autocomplete/5',
);

 $output = drupal_get_form('find_a_lawyer', $form);
 return $output;
}

function find_a_lawyer_submit ($form_id, $form_values) {
   $vocabulary = $form_values['vocab'];
  // $arg1 = $form_values[$vocabulary->vid];
  
  $arg1 = $form_values['tags'];
  return 'node/FindLawyer/'.$arg1;  // your redirect path here.  or use drupal_goto().
}

... I explicitly named that form element as just 'tags'. And in the submit routine I assigned $arg1 to that key/value...

and it worked.... I'm still not sure why the tutorial (http://drupal.org/node/42552 ) on using autocomplete had the last tree element as [$vocabulary->vid] , or what that actually resolves to.. I tried printing all POST and GET variables on submit, and it wasn't showing anything.

Thank though. Problem solved...
-Chris