0

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;
}
  • you mentioned that "the result will be displayed on another page" you mean, you need a separate URL for that? – Krishna Mohan Mar 17 '14 at 8:06
1

I suggest:

  • deleting all the default submit handlers in the submit array in the $form variable - to stop the data being stored in the database
  • add your own submit handler to do the calculation and store the result in the standard PHP _SESSION variable
  • add a #redirect property in the (correct place) $form array variable to redirect the to URL of the result page
  • write code for the result page output the result in the _SESSION variable. To do this determine what type of page will display the result so that you can implement appropriate code to display the result - there are several ways to do it: e.g. code in a template file, or a custom programmatically created block
| improve this answer | |
1

The "quick and dirty" way to display the result of the addition is to add the following line at the end of your submit handler:

drupal_set_message($c);

That will display a status message above the form with the result.

But if you want to display a new page with the result, I suggest redirecting the form to a new menu item. Thus your submit handler will look like this:

function form_price_form_submit($form, &$form_state) {
$a = $form_state['values']['numbera'];
$b = $form_state['values']['numberb'];
$c = $a + $b;
$form_state['redirect'] = 'form-price-result/' . $c;
}

And you will need to add the following menu item for function form_price_menu:

$items['form-price-result/%'] = array(
'title' => 'Result of price calculation',
'page callback' => 'show_result',
'page arguments' => array(1),
'type' => MENU_CALLBACK,
'access arguments' => array('submit form_price'),
);

And the following callback function to display the result:

function show_result($result){
   return t($result); }
| improve this answer | |

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.