0

in my form i have to show two radio button and one submit button, for example

candidate(radio)

organization(radio)

submit button.

my requirement is that

if user click/check on candidate and submit then candidate form should be open.

or

if user click/check on organization and submit then organization form should be open.

my code:

<?php
/**@file
*donation.module*/
/**
*Implements hook_menu()*/
function donation_menu(){
$items['donate'] = array(
'title' => 'Donate',
'page callback' => 'drupal_get_form',
'page arguments' => array('donation_donate_form'),
'access arguments' => array('access content'),
'access callback' => TRUE,
);    
return $items;
}
/**
*Implements hook_form().
*/
function donation_donate_form($form, &$form_submit){
/*the options to display in our form radio buttons*/
$options = array
'candidate' => t('Candidate'),    
'organization' => t('Organization'),
);
$form['officials'] = array(    
'#title' => t('OFFICIALS'),    
'#type' => 'radios',
'#options' => $options,    
'#default_value' => $options['candidate'],
);
$form['name'] = array(
'#title' => t('NAME'),
'#type' => 'textfield',    
);
$form['submit'] = array(
'#value' => t('PROCEED'),
'#type' => 'submit',
);
return $form;
}
1
  • No need to crosspost the exact same question to both StackOverflow and here. Just pick one site or other other.
    – mpdonadio
    Commented Oct 7, 2014 at 13:43

2 Answers 2

0

I'd do something like

function donation_donate_form_submit($form, &$form_state) {
  switch ($form_state['values']['donate']) {
    case 'candidate':
      $form_state['redirect'] = 'donate/candidate';
      break;

    case 'organization':
      $form_state['redirect'] = 'donate/organization';
      break;
  }
}

and from this point I can do two things

Solution 1

Create other two menu items with different form for each

Solution 2

your original form would be called if you don't create any extra menu items (drupal falls back to the longest menu item that match the current URL), then change the existing form depending on the argument like this

function donation_donate_form($form, &$form_submit) {
  $form = array();
enter code here
  // No argument ? we must be in the selection phase.
  if (arg(1) == NULL) {
    $form['officials'] = array(
      '#title' => t('OFFICIALS'),
      '#type' => 'radios',
      '#options' => array(
        'candidate' => t('Candidate'),
        'organization' => t('Organization'),
      ),
      '#default_value' => 'candidate',
    );

    $form['name'] = array(
      '#title' => t('NAME'),
      '#type' => 'textfield',
    );

    $form['submit'] = array(
      '#value' => t('PROCEED'),
      '#type' => 'submit',
    );
  }
  elseif (arg(1) == 'organization') {
    // Candidate form stuff.
  }
  elseif (arg(1) == 'candidate') {
    // Organization form stuff.
  }
  return $form;
}

It depends on how you like your code, and what you are trying to do.

-1

I'm not too clear on what you mean by "candidate form should open". You did say after they press submit so can I assume you mean they go to the next page in some process? OR do you mean you want some other dynamic content to load on the same page?

IF you simply want to change the action of the form based on which radio is selected then something like this would do the trick,

<script type="text/javascript">

function chooseAction(form,radioname){

    var radios = document.getElementsByName(radioname);

    for(var i = 0; i < radios.length; i++){

        if (radios[i].checked){

            if (radios[i].value == 'candidate'){
                form.action = "can.php";
                return true;
            }
            if (radios[i].value == 'organization'){
                form.action = "org.php";
                return true;
            }

        }

    }

    return false;

}


</script>



<form id="myform" name="myform" method="get" action="" onSubmit="return chooseAction(this, 'officials')">
Candidate:<input type="radio" name="officials" value="candidate"><br>
Organization:<input type="radio" name="officials" value="organization"><br>
<input type="submit" value="go">
</form>

i suppose this might not be what youre looking for if you're looking for how to lay all this out automatically somehow in drupal

2
  • if i click/check on candidate radio button and click on submit button then candidate form should be open on candidate page. Commented Oct 4, 2014 at 1:32
  • It should rather be a Drupal Behavior, not pure script.
    – Mołot
    Commented Oct 7, 2014 at 13:49

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.