3

I'm new to Drupal and things that seem easy are apparently abstracted to oblivion.

I've created a module that displays a survey form. I would like to collect the data and store it in the database, and then allow the admin of the site to view the data that has been collected.

What is the "Drupal way" to do this?

Thank you in advance!

<?php

function member_survey_menu() {

  $items['member_survey'] = array(
    'title' => t('Member Survey'),
    'page callback' => 'member_survey_form',
    'access arguments' => array('access content'),
  );
  return $items;

}

function member_survey_form(){

    $output = drupal_get_form('member_survey_my_form');
    return $output;
}

function member_survey_my_form($form_state){
    # the options to display in our form radio buttons
    $options1 = array(
      'Strongly Agree' => t('Strongly Agree'),
      'Agree' => t('Agree'), 
      'Neutral' => t('Neutral'),
      'Disagree' => t('Disagree'),
      'Strongly Disagree' => t('Strongly Disagree'),
    );

    # the options to display in our form radio buttons
    $options2 = array(
      'Very Satisfied ' => t('Very Satisfied '),
      'Satisfied' => t('Satisfied '), 
      'Neither Satisfied or Dissatisfied' => t('Neither Satisfied or Dissatisfied'),
      'Dissatisfied ' => t('Dissatisfied '),
      'Very Dissatisfied ' => t('Very Dissatisfied '),
    );

    $form['knowledgable'] = array(
      '#type' => 'radios',
      '#title' => t('Question 1'),
      '#options' => $options1,
      //'#default_value' => $options['Neutral'],
    );

    $form['satisfied'] = array(
      '#type' => 'radios',
      '#title' => t('Question 4'),
      '#options' => $options2,
      //'#default_value' => $options['Neutral'],
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );

  return $form;
}

// Validate the form submission
function member_survey_my_form_validate($form, &$form_state){
    if(empty($form_state['values']['knowledgable']) ||
        empty($form_state['values']['satisfied']) 
    ){
        form_set_error('knowledgable',t('Please respond to all questions, thank you!'));
    }

} 

?>
4
  • what guidance are you using to achieve this task? what are you doing to catch the errors? what are/is the error message/s? do you have idea about what part/s of your task have errors?
    – cigotete
    Sep 21, 2011 at 16:30
  • 1
    I'm not sure what you mean by "guidance". I have a form, it works, but I need to store the results, and later retrieve. I've updated the question to show the code.
    – kylex
    Sep 21, 2011 at 16:48
  • There any reason why you don't like any of the contrib modules that currently exist?
    – chrisjlee
    Sep 21, 2011 at 17:02
  • @Chris, Part of this is a learning exercise as well. Maybe it's just Drupal's way, but dear god, creating a form and storing the data should not be a &*#$& ordeal.
    – kylex
    Sep 21, 2011 at 17:08

5 Answers 5

2

A specific answer: you can use sumbit hook to store data. see Submitting Forms: http://drupal.org/node/751826

A general answer: follow this guide related with creating, theming, validating, submitting, storing data, and understanding the flow in Drupal 6 forms: Form API Quickstart Guide http://drupal.org/node/751826

to retrieve data you can use these guidances:

db_query documentation http://api.drupal.org/api/drupal/includes--database.pgsql.inc/function/db_query/6

Database abstraction layer http://api.drupal.org/api/drupal/includes--database.inc/group/database/6

also you can review Examples module http://drupal.org/project/examples specifically the module dbtng_example to database examples or page_example to build a static page add your retrieved information. please note that dbtng_example module is only for Drupal 7, but reviewing this module you will have more ideas about what to review in Drupal 6.

also could be useful the "Module developer's guide" to dive in tutorials/examples about how build forms and retrieve data using modules http://drupal.org/developing/modules

3
  • The specific answer doesn't give me an answer: "The practical upshot to this is that many developers immediately find themselves asking the question of "where does my data get stored?". The answer is simply that it doesn't" Ummm... okay, but I want it stored. How do I do that. ::grumbles::
    – kylex
    Sep 21, 2011 at 17:04
  • Hi Kypex, I think that the specific answer give a guide. the submit hook allows insert your data, specifically the db_query use the SQL declaration to insert your data. But first, to store your data in your database, you must have where you store data. in your database you can build your own table to insert data. see this guidance about SQL insert tizag.com/sqlTutorial/sqlinsert.php. here a guidance about how to create a table tizag.com/sqlTutorial/sqltables.php. I hope have understood your case.
    – cigotete
    Sep 21, 2011 at 17:23
  • so there's no inherent way to do this in Drupal? I create my own table, dump the data into that table. There's no store forms in database hook or anything?
    – kylex
    Sep 21, 2011 at 19:05
2

What you need is the form submission handler, which is the function called when the form is submitted. By default, Drupal uses a function that is named after the form build handler, with "_submit" appended. In your case, that function is member_survey_my_form_submit(), and it takes the same parameters passed to member_survey_my_form_validate().

As for saving the data, you first need to decide the structure of the database table you use to save the data; in particular you need to decide which primary keys to use.
Considering the form is for a survey, and supposing each user can submit just a survey, then I would use the user ID as primary key. The database table would have a structure similar to the following one:

  • Fields
    • uid
    • knowledgable
    • satisfied
  • Primary keys
    • uid

For the code, I am also making the assumption users are not allowed to edit their survey; the code just avoid saving the data if there is already data for that user ID.

function member_survey_my_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $values['uid'] = $GLOBALS['user']->uid;

  if (!db_result(db_query("SELECT 1 FROM {survey} WHERE uid = %d", array($values['uid'])))) {
    // There isn't a database row for that user.
    drupal_write_record('survey', $values);
  }
} 

Replace "survey" with the database table name.

1

I don't know if you are still looking for anwser but Webform module covers your case. From docs:

Webform is the module for making surveys in Drupal. After a submission, users may be sent an e-mail "receipt" as well as sending a notification to administrators. Results can be exported into Excel or other spreadsheet applications. Webform also provides some basic statistical review and has and extensive API for expanding its features.

To see what data is stored in DB just log to database and do a query SELECT * FROM webform_submitted_data;.

1

Make an install file first. Create your schema for form. Then in the .module-file, write insert query in submit handler of form:

function _my_component_add_form_store_edit_submit($form, &$form_state) {
  if(isset($form_state['values']['cid'])) {
    db_update('mid_components')
      ->fields(array(
        'title' => $form_state['values']['human_name'],
        'machine_name' => $form_state['values']['name'],
        'description' => $form_state['values']['description'],
        'options' => $form_state['values']['wizard_key'],
      ))
      ->condition('cid', $form_state['values']['cid'] )
      ->execute();
    drupal_set_message(t('Record has been updated.'));    
  }
  else {
    // Here u can insert Your custom form values into your custom table.
    db_insert('mid_components')
      ->fields(array(
        'title' => $form_state['values']['human_name'],
        'machine_name' => $form_state['values']['name'],
        'description' => $form_state['values']['description'],
        'options' => $form_state['values']['wizard_key'],    
      ))->execute();
    drupal_set_message("successfully saved details.");    
  }
  //$form_state['redirect'] = 'admin/people/survey'; 
}
0

This is the PERFECT series tutorials about DRUPAL 6 form, module and table. I think, it will help you so much: http://webemania.com/blog/how-to-create-custom-form-in-drupal

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.