[development] submitting and processing a form!

Robrecht Jacques robrechtj+drupal at gmail.com
Tue Jul 4 14:42:27 UTC 2006


On 7/4/06, Augustin (Beginner) <drupal.beginner at wechange.org> wrote:
>
> In the code below, I create a hook_menu() callback to the function
> formtest()
> with a form.
> I want to be able to print some information according to the form that has
> just been submitted (here, a simple text string), then reprint the form
> below
> that.
> I know that $_POST['op'] is set when I submit (and the process die()s when
> I
> uncomment that line), but I can't get the information submitted to be
> displayed above the form.
>
> I have tried using the function formtest_submit() but returning a value
> there
> doesn't get the expected result (it seems it's a path callback).


That's correct.

I have searched the extensive documentation, but all there is is how to
> build
> a form, not how to process and display the information.
>
> Am I, for the third time this week, missing something obvious and  simple?


You need to save the data somewhere. There are a number of possibiilites:
- save it in $_SESSION,
- redirect to a path with the text attached to the url),
- save it in a variable or other db table,
- disable redirecting.
(there may be more options)

BTW: your data is in $_POST['edit']['text'] not $_POST['text']

So your example code becomes, if you disable redirects (last option):

<?php
function test_menu($may_cache = FALSE) {
  $items = array();
  $items[] = array(
    'path' => 'test/formtest',
    'title' => t('Test form api'),
    'access' => TRUE,
    'callback' => 'test_formtest',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

function test_formtest() {
  $form = array();

  if (!empty($_POST['edit']['text'])) {
    $form['entered-text'] = array(
      '#type' => 'item',
      '#title' => t('Entered text'),
      '#value' => $_POST['edit']['text'],
    );
  }

  $form['text'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter some text'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Enter'),
  );

  return drupal_get_form('test_formtest', $form);
}

function test_formtest_submit($form_id, $form_values) {
  return FALSE;
}
?>

Depending on your case, you may need to test inside _submit() which button
is pressed (using $_POST['op']) and redirect to another page depending on
the button (eg "Preview" or "Submit").

You may prefer to save the data in $_SESSION if you want the data to be
available again when they go to the form again, the _submit() then becomes
(with some small changes to the form obviosuly):

<?php
function test_formtest_submit($form_id, $form_values) {
  $_SESSION['test_formtest'] = $form_values;
}
?>

Hope this helps,
Robrecht
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20060704/b770fb30/attachment.htm


More information about the development mailing list