I have created a module that writes a block with a very simple form in it.

The purpose of it is calculating distance between 2 adresses, one fixed, one asked by the form.

Now I can't get the results to show in the block itself, and I can't find any tutorials on how to do that.

The block shows, the form is in it also, but when i submit a postalcode it seems that nothing happens (also the postcode input field resets to emtpy).

the code that retrieves the distance works also (tested on commandline).

How can I approach this?

The code for my block:

  function reiskosten_block_view($delta = '') {
  $block = array();

  switch ($delta) {
  case 'reiskostenblock':
  $block['subject'] = t('Reiskosten berekenen');
  $block['content'] = drupal_get_form('reiskosten_form');

The code for submitting the form:

function reiskosten_form_submit($form, &$form_state) {

  $postcode = str_replace(' ','',$form_state['values']['postcode']);

  $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=1024AA|Netherlands&destinations=$postcode|Netherlands&mode=car&language=nl-NL&sensor=false&key=myKey";

$data = drupal_http_request($url);
$result = drupal_json_decode($data->data);

$destination = $result['destination_addresses'][0];
$distance = $result['rows'][0]['elements'][0]['distance']['text'];
$duraton = $result['rows'][0]['elements'][0]['duration']['text'];

  $form_state['rebuild'] = TRUE;

I want the output of $result to be shown below the submitbutton. It is driving me crazy this one, been puzzling for 5 hours now on this thing (what should be relatively simple imho).

edit: I also validate on postalcode with:

function reiskosten_form_validate($form, &$form_state) {
  if ( empty($form_state['values']['postcode']) ) {
    form_set_error('postcode', t('Postcode mag niet leeg zijn'));

It doesn't throw an error so it seems to me that 'postcode' has a value and the function should be processed.

1 Answer 1


Solved it. Solution:

in function reiskosten_form_submit($form, &$form_state) {

$result = drupal_json_decode($data->data);
$form_state['results'] = $result;

in function reiskosten_form($form, &$form_state) {

$form['destination'] = array(
  '#markup' => '<div>' . $form_state['results']['destination_addresses'][0] . '</div>',

Wrap t in an if to see if the values are there and the magic happens. Pfff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.