Hi,

I am new to Drupal. I couldn't figure out how to display the values submitted in a form on a new page without storing in a database.
I found a video on how to retrieve data submitted through a form and stored in a database, but, for my site I wouldn't need to store data. The data will get some simple mathematical processing and then the result will be displayed on another page. Here is the code I have.

<?php
/**
 * Implements hook_permission().
*/
function form_price_permission() {
  return array(
    'submit form_price' => array(
      'title' => t('Submit form_price'),
      'description' => t('Submit the form_price form'),
    ),
  );
}
/**
 * Implements hook_menu().
*/
function form_price_menu() {
    $items = array();
	$items['form-price'] = array(
	  'title' => 'Form to calculate price',
          'page callback' => 'drupal_get_form',
	  'page arguments' => array('form_price_form'),
	  'access arguments' => array('submit form_price'),
    );
    return $items;
}
 /**
  * form implementation
 */
 function form_price_form($form,&$form_state){
 
	$form = array();
	$form['numberb'] = array(
		'#required' => '1',
		'#field_prefix' => 'Number B',
		'#weight' => '1',
		'#title' => t('B'),
		'#type' => 'textfield',
	);
	$form['numbera'] = array(
		'#required' => '1',
		'#field_prefix' => 'Number A',
		'#weight' => '2',
		'#title' => t('A'),
		'#type' => 'textfield',
    );
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Add item'),
		'#weight' => '3',
	);
return $form;
}
/**
  * how to display A + B
 */
function form_price_form_submit($form, &$form_state) {
	$a = $form_state['values']['numbera'];
	$b = $form_state['values']['numberb'];
	$c = $a + $b;
}

Thank you for your answer.

Comments

yenyeskutty’s picture

Hi,

It needs a small change, call a View with arguments from the form. For example, imagine you have a View which takes two arguments, a and b. The form could call this View based on the values the user has entered. Then in the view you just do your calculation and display the value.

function form_price_form_submit($form, &$form_state) {
$a = $form_state['values']['numbera'];
$b = $form_state['values']['numberb'];
drupal_goto("/my_view/".$a."/".$b);
}

johnpk’s picture

$form_state['storage'] is probably what you're looking for.

function form_price_calculate_result($form, &$form_state) {
        $form_state['storage']['numbera'] = $form_state['values']['numbera'];
        $form_state['storage']['numberb'] = $form_state['values']['numberb'];
        $form_state['storage']['result'] = $form_state['values']['numbera'] + $form_state['values']['numberb'];
        $form_state["rebuild"] = TRUE;
}

/**
  * form implementation
 */
 function form_price_form($form,&$form_state){
	$form = array();
       if(!isset($form_state['storage']['numbera'])) {
         /* This part of the form will have the values to be submitted */
          $form['numberb'] = array(
		'#required' => '1',
		'#field_prefix' => 'Number B',
		'#weight' => '1',
		'#title' => t('B'),
		'#type' => 'textfield',
	  );
	  $form['numbera'] = array(
		'#required' => '1',
		'#field_prefix' => 'Number A',
		'#weight' => '2',
		'#title' => t('A'),
		'#type' => 'textfield',
          );
          $form['actions']['calculate_result'] = array(
               '#type' => 'submit',
               '#value' => t('Submit'),
               '#submit' => array('form_price_calculate_result'),
          );   
        }
        else {
	   /*this part of the form has submitted values in $form_state['storage']['numbera'], $form_state['storage']['numberb'] and                 $form_state['storage']['result'] so that you can display in this part of the form.*/
      
            // display the values here.......
           $form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Add item'),
		'#weight' => '3',
	   );
         }
   return $form;
}