Search code examples
drupalvariablesformsdrupal-6drupal-fapi

Drupal 6 passing variables from Forms to Content, how to?


I created a BLOCK (left) with this simple form. Now I want to PROCESS and DISPLAY results on PAGE (center) How can I do it ?

inputs:

name = James
surname = Bond

output I want :

<div style="color:red">Welcome, James Bond</div>

here is a BLOCK which i wrote and works.

<?php

echo drupal_get_form('myForm');

function myForm($form_state) {
 $form['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#size' => 20,
  '#maxlength' => 10
 );
 $form['surname'] = array(
  '#type' => 'textfield',
  '#title' => t('Surname'),
  '#size' => 20,
  '#maxlength' => 10
 );
 $form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Save')
 );
return $form;
}

function myForm_submit($form,&$form_state)
{
 //??
};

Now I need to display the output :).

Please don't suggest to use VIEWS or any other addon.

I want to learn Drupal from Inside out. Not the other way around ;)


Solution

  • Well, it depends a bit on how you want to do things. Since you are learning how to make a drupal module, you might want to start with an implementation of hook_menu(). This hook is used to define menu items, which basically means that you can register urls with that function. Going that route you can:

    1. Implement hook_menu()
    2. A general way of handling redirects is using drupal_goto(). However, in this case it is much more fitting to use the $form_state['redirect'] as Henrik explained in his comment.
    3. For the url you are redirecting to, you should have a call back function which is where you put your logic, the way you setup the hook_menu and the callback function will determine how you get your variables available. You probably want to look into the arg() function which is what generally is used to get the values from the url.
    4. Run the user input through a filter to make sure that they haven't posted nasty stuff like script tags ect, use check_plain
    5. return a theme function, alternatively make your own, look at theme() and hook_theme()

    There are quicker ways to do this, but doing it this way, you will generate urls for every search result that drupal can cache, which is nice. Also not being dependent on the post parameters people can bookmark the search results

    Another thing is that you might want to put some basic validation to your form. That would be a good practice to learn. That would look something like this:

    /**
     * Validation handler for myForm.
     */
    function myForm_validate($form, &$form_state) {
        $name = $form_state['values']['name'];
        // do some checks to $name.
        if ($error) {
            form_set_error('name', t('error message to be displayed, showing the value of the field: @name', array('@name' => $name);
        }
    };