views:

986

answers:

3

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 ;)

+3  A: 

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);
    }
};
googletorp
It should be noted that `drupal_goto` is called implicitly after the last form submit function, redirecting to the path found in `$form['#redirect']`. By default this will be the page the form was submitted from, but it is common to adjust this in the submit function. In this case, one way of passing the variables to a page callback would be to put them in that path, e.g. `$form['#redirect'] = 'welcome/' . $form_state['values']['name'] ...` . The callback for 'welcome/%' would then create the output, using the passed values.
Henrik Opel
Thanks, I forgot about the redirect argument in the $form. Added it to my answer.
googletorp
**Oups - a correction:** According to docs, one should set $form_state['redirect'] in submit functions (other parameter, no hash sign) instead of $form['#redirect'] (this is just for defining the wanted redirection on form creation)
Henrik Opel
@googletorp: I took the liberty to put this correction in your answer also - hope that's ok.
Henrik Opel
Yeah, that's fine, I just took your word for anyways. I could remember the attribute actually, have had some problems with it in the past, where it didn't always redirect to the correct url on my local dev sites though.
googletorp
A: 

You could implement AHAH in your form, and specify an element inside your page's content area as the 'wrapper' (the element in which the results of the callback function will be placed). But, you would need to understand the excellent advice of Mr. Opel before you even attempt it.

threecheeseopera
A: 

How about using drupal_set_html_head() to write a string of script to the head section of the page? I am doing this on specific pages (getting user latitude and longitude and passing them into my gMap function), but I am interested in dong the same thing directly from hook_form_submit(). I have made a few tries at it, and I am obviously outputting the script string but calling the function from submit doesn't seem to work.

GoodNews