Community Documentation

Develop a simple block module with a simple form

Last updated October 26, 2009. Created by coupet on April 4, 2006.
Edited by esmerel, sepeck. Log in to edit this page.

Develop a simple block module with a simple form. The form has 2 textfields and the values should be saved to a db table. The block developed with the 4.7 form api. Here's the code:

Special thanks to Meinolf Droste, (aka meinolf) for this tip.

<?php
function quickmail_perm(){
    return array (
'can send quickmail');
}

function
quickmail_block($op = "list", $delta = 0) {
  if (
$op == 'list') {
   
$block[0]['info'] = t('Quickmail Block');
    return
$block;
  }
  elseif (
$op == 'view') {
     
$block['subject'] = 'Quickmail';
     
$block['content'] = quickmail_form();
      return
$block;
  }
}

function
quickmail_form($edit = null) {

 
$form['details']['firma'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Firma'),
   
'#default_value' => $edit['firma'],
   
'#size' => 15,
   
'#maxlength' => 128,
   
'#description' => null,
   
'#attributes' => null,
   
'#required' => true,
  );
 
$form['details']['telefon'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Telefon Nr.'),
   
'#default_value' => $edit['telefon'],
   
'#size' => 15,
   
'#maxlength' => 128,
   
'#description' => t('Geben Sie hier die Telefon Nr. ein unter der Sie unseren Rückruf erwarten.'),
   
'#attributes' => null,
   
'#required' => true,
  );

 
$form['details']['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Senden'),
   
'#submit' => TRUE,
  );
  return
drupal_get_form('quickmail_form', $form);
}

function
quickmail_form_submit($form_id, $form_values) {

 
db_query("INSERT INTO {callback} (firma, telefon) VALUES ('%f', '%t')", $form_values['firma'], $form_values['telefon']);

drupal_goto('thank_you_page');//the page the user should see, after submit the form.
}
?>

Comments

I don't put

this (my) code into the handbook.

I think it could be a little bit confusing what this module actually is good for. I use it on my site www.mdwp.de as a little service for my customer.
A customer can fill out the form, i get a message and will call him back (via phone) at my cost.

Maybe it's better to name this module ('callback_service') or something.
But it should be clear, it has nothing to do with Drupals callback functions.

There is a error in the quickmail_block function.
Delete the last 'else block' because there is no 'quickmail_submit' function in the code.
The code should look like this:

function quickmail_block($op = "list", $delta = 0) {
  if ($op == 'list') {
    $block[0]['info'] = t('Quickmail Block');
    return $block;
  }
  elseif ($op == 'view') {
      $block['subject'] = 'Quickmail';
      $block['content'] = quickmail_form();
      return $block;
  }
}

And of course the last line should look like this

   drupal_goto('thank_you_page');//the page the user should see, after submit the form.

Meinolf Droste
--
http://drupal.mdwp.de
http://mdwp.de
http://go-with-us.de

permissions

You're defining a permission, maybe you should also use it :)
...elseif ($op == 'view' && user_access('can send quickmail'))...

Page status

About this page

Drupal version
Drupal 4.7.x

Archive

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.