Hello guys,

I have read Forms API Quick Start Guide and some other stuff, but am unable to understand where to put this code. I have created a page under "Create Content --> Page", put some code to create a field in the body, selected PHP from Input format, but it does not work.

Could anybody tell me please where to put this code to create a personal form or to modify a form generated with webform.
Or point me to any link which explains in detail.

I will be thankful guys.

Comments

Island Usurper’s picture

The key to getting the Forms API to work is calling drupal_get_form(). I hope you're using Drupal 5.x, because there were some critical changes made to it, and that's all I know how to explain.

In your page, you should have a function to construct and return the form array. Outside of that function, call drupal_get_form('page_form'); (page_form is whatever you named your function.) If you want to submit the form to the same page, you should also have a page_form_submit() function, and possibly a page_form_validate() function.

Modifying an existing form really requires writing a module that implements hook_form_alter().

-----
Übercart -- One cart to rule them all.

awan’s picture

Thanks Island Usurper for reply,

I am using Drupal 5.1.

I have copied the following code from "Forms API quickstart guide"



<?php
function test_form() {
  // Access log settings:
  $options = array('1' => t('Enabled'), '0' => t('Disabled'));
  $form['access'] = array(
    '#type' => 'fieldset',
    '#title' => t('Access log settings'),
    '#tree' => TRUE,
  );
  $form['access']['log'] = array(
    '#type' => 'radios',
    '#title' => t('Log'),
    '#default_value' =>  variable_get('log', 0),
    '#options' => $options,
    '#description' => t('The log.'),
  );
  $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
  $form['access']['timer'] = array(
    '#type' => 'select',
    '#title' => t('Discard logs older than'),
    '#default_value' => variable_get('timer', 259200),
    '#options' => $period,
    '#description' => t('The timer.'),
  );
  // Description
  $form['details'] = array(
    '#type' => 'fieldset',
    '#title' => t('Details'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['details']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Describe it'),
    '#default_value' =>  variable_get('description', ''),
    '#cols' => 60,
    '#rows' => 5,
    '#description' => t('Log description.'),
  );
  $form['details']['admin'] = array(
    '#type' => 'checkbox',
    '#title' => t('Only admin can view'),
    '#default_value' => variable_get('admin', 0),
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#size' => 30,
    '#maxlength' => 64,
    '#description' => t('Enter the name for this group of settings'),
  );
  $form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  return $form;
}

function test_page() {
  return drupal_get_form('test_form');
}
?>

and pasted it to "body field" of a page.

But when I "Submit my page It does not display anything."

Am I missing something ??

nevets’s picture

Functions defined in code inside a node body only exist if the node is veing viewed. For the form to do something you need a function to handle the submit (In your case it would be called ('test_form_submit'). This function needs to exist when the form is submitted or it is not called so it should exist within a module.

awan’s picture

Thanks nevets,

I have seen the handbook for writing a module. It is quite vast.

Do you think it is necessary to read all this to just use Forms API??

Or if somebody has some time to just explain me how to write a module (in some steps) to create a form with Forms API. It would be grateful.

I am using Windows XP, Easyphp 1.8 and Drupal 5.1

Thanks a lot

se7en’s picture

Ran across this post while searching for some issues I'm having. I had a lot of similar questions and found the following helpful for creating a basic module which I could then edit with the code for a form:

http://drupal.org/node/84658

dantina’s picture

Hi Nevets,

I've got a problem with your example here http://drupal.org/node/68159, and have posted a reply there which is as follows:

-----------------------------------
When I tried to use the example (http://drupal.org/node/68159), I got the following error:

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'myform_sample' was given in ...\includes\form.inc on line 218.

Any idea what I am doing wrong?
-----------------------------------

I am just hoping you'll see it soon and help. Thanks.

nevets’s picture

The error would suggest you are using Drupal 5, the example is for 4.7 and there where some changes to the form API in Drupal 5. See http://drupal.org/node/64279#drupal-get-form for the details.