I have been trying to solve a fairly simple question. My hunch is that someone here has some sample code that I can look at.
I want to do a single form with 5 YES/NO questions.
The submit button will add up all of the yes's, to create nValQuestions.
so nValQuestions can be between 0 to 5.
On submit, the page will route to...

page0 if nValQuestions = 0
page1 if nValQuestions is 1,2.
Page3 if nValQuestions is 3,4,5.

And I want this to be Drupal 7
could someone show me some example code that would do this please.
thanks

Comments

sbanerjee.302’s picture

function test_menu(){
	$items['test/test-form'] = array(
		'title' => 'Sample Form', 
		'page callback' => 'drupal_get_form', 
		'page arguments' => array('test_form'),		
		'access callback' => TRUE, 
		'type' => MENU_NORMAL_ITEM,
	);
	
	return $items;
}


function test_form($form, &$form_state){	
	
	$form  = array();
	
	for($i = 0; $i < 5; $i++){
		
		$form['chkb-'.$i] = array(
			'#type' => 'radios',
			'#options' => array(1 => t('Yes'), 0 => t('No')),
			'#title' => t('What is your answer to question no. '.($i+1).' ?'),
		);	
		
	}
	
	$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));

	return $form;
}

function test_form_submit($form, &$form_state){	
	
	$nValQuestions = 0;
	
	for($i = 0; $i < 5; $i++){
		if($form_state['values']['chkb-'.$i] == 1){
			$nValQuestions += $form_state['values']['chkb-'.$i];	
		}
	}
	
	switch($nValQuestions){
		case 0:
			drupal_goto('http://www.google.com');
		case 1:
		case 2:
			drupal_goto('http://www.msn.com');
		case 3:
		case 4:
		case 5:
			drupal_goto('http://www.bing.com');
	}
}