0

I have developed a custom form in my module. Now I have to change the form action based on radio button selected. Suppose if I have 3 radio buttons saying 1. ABC 2. DEF 3. XYZ

If i select ABC and submit form, my result should display on www.domainname.com/abc

Thanks.

1

You can change the Form action in submit function

    function your_form_name_submit($form,&$form_state){
          if($form_state['values']['radion_button_name'] == 'abc') {
         $form_state['redirect'] = url('path/to/redirect');
        }
    else{
       $form_state['redirect'] = url('path/to/redirect');
     }
}

hope this will help you!!

  • Would be nice if you wold mention that it will not change form's action in HTML sense, only the page user sees at the end. – Mołot Jan 13 '16 at 13:27
1

There may be following options to change the form action based on radio option selection.

1. Using ajax callback

Create menu item

/**
 * Implements hook_init().
 */
function YOUR_MODULE_menu(){
  $items = array();
  $items['stack-exchange'] = array(
    'title' => 'Stack exchange',
    'description' => 'Stack exchange.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('stack_exchange_form'),
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

// form callback
function stack_exchange_form($form, &$form_state){
  $form = array();
  $options = array('abc' => t('ABC'), 'def' => t('DEF'), 'xyz' => t('XYZ'));

   $form['submitto'] = array(
    '#type' => 'radios',
    '#title' => t('Submit to'),
    '#options' => $options,
    '#description' => t('Select a option to submit the form to specific location.'),
    '#ajax' => array(
      'callback' => 'alter_form_action',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit')
  );
  return $form;
}

// ajax call back
function alter_form_action($form, $form_state){
  $commands = array();
  $action = $form_state['values']['submitto'];
  switch($action){
    case "abc":
      $submit_to = 'your-path/' . $action;
      break;
    case "def":
      $submit_to = 'your-other-path/' . $action;
      break;
    case "xyz":
      $submit_to = 'your-again-other-path/' . $action;
      break;
  }
  $commands[] = ajax_command_invoke('form#stack-exchange','attr', array('action', $submit_to));
  return array('#type' => 'ajax','#commands' => $commands);
}

2. Using jquery

// attach js file in from callback
$form['#attached']['js'] = array(
    drupal_get_path('module', 'dev') . '/js/stack_exchange_form.js'
  );

and put following js code into

(function($) {
  Drupal.behaviors.YOUR_MODULE = {
    attach: function (context, settings) {
      $("form#stack-exchange-form input[name=submitto]:radio").change(function () {
        var action = $(this).val();
        var submit_to = '';
        switch(action){
          case "abc":
            submit_to = 'your-path/' + action;
            break;
          case "def":
            submit_to = 'your-other-path/' + action;
            break;
          case "xyz":
            submit_to = 'your-again-other-path/' + action;
            break;
        }
        $('form#stack-exchange-form').attr('action', submit_to);
      });
    }
  };
})(jQuery);

Note: you need to change the hook name and element selector accordingly

0

You can do this stuff in your form submit.

function yourmodule_form_submit($form, &$form_state) {
 // do your submit stuff
 if($form_state['values']['radion_button_name'] == 'abc') {
   drupal_goto('abc path');
 } else if ($form_state['values']['radion_button_name'] == 'efg') {
   drupal_goto('efg path');
 } else {
   drupal_goto('default path');
 }
}
  • I believe form_state should be preferred – Mołot Jan 13 '16 at 13:27
0

You can apply this code it will work for you:

 <?php

function formalter_menu() {
$items = array();

$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
'page arguments' => array('formalter_form'), //put the name of the form here
'access callback' => TRUE
);

return $items;
}

function formalter_form($form, &$form_state) {

$form['price'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'What is Your Price?',
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE, //make this field required 
);
$form['topic'] = array(

 '#type' => 'radios',
 '#title' => t('Topic'),
 '#options' =>          array('Movie'=>t('Movie'),'Songs'=>t('Songs'),'Ads'=>t('Ads'),'Tv  

Serials'=>t('Tv Serials'),'Others'=>t('Others')),
    );
  $form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
  );

  return $form;
 }

 function formalter_form_validate($form, &$form_state) {
 // dpm($form);
//dpm($form_state);
  if($form_state['values']['topic'] == "Movie")
   {
  $form_state['redirect'] = url('http://www.google.com');
     }
    else{
      $form_state['redirect'] = url('http://www.yahoo.com');
      }
  }

  function formalter_form_submit($form, &$form_state)
{
drupal_set_message('warning',"Thanks for submitting the form");

  }

 ?>

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.