Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By paramathone on
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
Calling a View with arguments from the form
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);
}
You can use a multi step form
$form_state['storage'] is probably what you're looking for.