Hi there,

I googled for 4 solid hours but did not find a solution. I want to embed a form in a drupal page node with the input format 'php code'. The form renders correctly but I am not able to provide a submit function nor am I able to get the form data in any other way.

  # for debugging
  print '<pre>'; print_r($_REQUEST); print_r($_POST); print_r($_GET); print '</pre>';

  $form = array();

  $form['name_first'] = array(
    '#title' => t('First name'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'register'
  );
  print drupal_get_form('form_register', $form);

  function form_register_submit($form_id, &$form_values) {
    # this is never called
    print '<pre>FOOOO! '; print_r($_REQUEST); print_r($_POST); print_r($_GET); print '</pre>';
  }

I found several examples where a standard html form is printed an the action redirected to an internal url, where it is handled by a module. I am somewhat hesitant to write a module just to process some simple register form for single use.

It would be highly appreciated if someone could point me to relevant documentation or provide an example.

Best regards
Stefan Radomski

Comments

NancyDru’s picture

I suppose you know there is a Webform module? And there are other modules that handle various kinds of registration.

Typically Drupal has a function that builds the form, then a separate (optional) function to validate the data, and finally (and also optionally) a function to handle the "submit." The hook_nodeapi can also take care of the submit/insert functions.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

sradomski’s picture

Dear nancyw,

thank you for your quick reply. I already wrote a bunch of custom drupal modules and am familiar with the concepts of the drupal 4.7 form api. I don't want to install another module which adds yet another node type as the webform module does.

I want a form embedded in a drupal page node - I guess the problem is, that I cannnot provide a function for drupal_submit_form to call when $form_id.'_submit' or $callback is supposed to be called as the php code in the page node gets eval'ed after the control flow passed drupal_submit_form (from http://api.drupal.org/api/4.7/function/drupal_submit_form):

  # ...
  if (isset($form['#submit'])) { 
    foreach ($form['#submit'] as $function => $args) { 
      if (function_exists($function)) { 
        $args = array_merge($default_args, (array) $args); 
        // Since we can only redirect to one page, only the last redirect will work 
        $redirect = call_user_func_array($function, $args); 
        if (isset($redirect)) { 
          $goto = $redirect; 
        } 
      } 
    } 
  } 

which is called by http://api.drupal.org/api/4.7/function/drupal_get_form:

  # ...
  if (!isset($form['#submit'])) { 
    if (function_exists($form_id .'_submit')) { 
      // we set submit here so that it can be altered but use reference for 
      // $form_values because it will change later 
      $form['#submit'] = array($form_id .'_submit' => array()); 
    } 
    elseif (function_exists($callback .'_submit')) { 
      $form['#submit'] = array($callback .'_submit' => array()); 
    } 
  } 

Does anyone know how to provide the submit function for drupal_submit_form from within a page node? The eval manual page says:

Also remember that variables given values under eval() will retain these values in the main script afterwards.

I was hoping that the same is true for functions defined in eval'ed code. I am still somewhat surprised that there is hardly any information about this topic as it feels like a common problem.

sradomski’s picture

Oh my .. it was working the whole time, the problem seems to be, that you can not print strings in the $form_id.'_submit' function. Below is a code sample to embed a form into a drupal page node:

  function foo_register_submit($form_id, &$form_values) {
    drupal_set_message($form_values['name_first']);
  }

  $form = array();

  $form['name_first'] = array(
    '#title' => t('First name'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'register'
  );
  print drupal_get_form('foo_register', $form);

Straight forward and just as you'd expect it .. Sorry for the confusion!

zahidansari’s picture

I am using 6.x and have written this code

<?php
  function testform_submit($form_id, &$form_values) {
    drupal_set_message("this is the text box value".$form_values['name_first']);
  }

function testform($formstate){

  $form['name_first'] = array(
    '#title' => t('First name'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'register'
  );

return $form;
}

echo drupal_get_form('testform');

When I am submitting the form only "this is the text box value" is printing !!

Am I using the wrong variable that holds all values?

kingandy’s picture

Most of the code snippets in this thread relate to Drupal 5 or earlier. In Drupal 6, the arguments of the _submit hook changed from ($form_id, $form_values) to ($form, &$form_state) - the old $form_values array now lives on $form_state['values']. Try this:

  function testform_submit($form, &$form_state) {
    drupal_set_message("this is the text box value". 
               $form_state['values']['name_first']);
  }

++Andy
Developing Drupal websites for Livelink New Media since 2008