1

Ok I don't know why I am having such a hard time getting my mind around how to do this. How do you access posted values in your page callback? Something in Drupal seems so hard. All examples just call drupal_get_form() but what do I do if I want to display the form and any results from the submit?

function stock_quotes_menu(){
    $items['stock-quotes'] = array(
     'title' => variable_get('market-summary-title', 'Stock Quotes'),
     'access arguments' => array('access content'),
     'page callback' => 'stock_quotes_page',
    );

    return $items;
}

function stock_quotes_page() {
    $symbols = isset($_REQUEST['symbol']) ? strtoupper($_REQUEST['symbol']) : '';
    $out = "<pre>".print_r($_REQUEST,1);
    if ($symbols != '') {
        $start = NULL;
        $pos = strpos($symbols, ",");
        /*if($pos == false){
            $start = date('Y-m-d');
        }*/
        $quotes = stock_quotes_fetch_yql_quote($symbols, $start);
        $out = print_r($quotes, 1);
    }

    $form = drupal_get_form('stock_quotes_get_form', $symbols);
    $build['snippets'] = array(
      '#type' => 'node',
      '#title' => '',
      '#prefix' => drupal_render($form),
      '#markup' => $out
    );

    return $build;
}

function stock_quotes_get_form() {
    $form['symbol'] = array(
      '#title' => t('Symbol(s)'),
      '#type'  => 'textfield',
      '#required' => TRUE,
      '#description' => t('Enter Symbols or Symbols seperated by , to get quotes'),
      '#default_value' => !empty($form_state['values']['symbol']) ? $form_state['values']['symbol'] : 'Enter A Symbol',
    );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Get Quote'
    );

    return $form;
}
1

By default, Drupal looks for two functions on form submission (for forms generated with the Form API): {$form_name}_validate() and {$form_name}_submit();

For what you are trying to do, you need to implement the submit handler, do something with the form values (store to session, database, etc) and then go to page that will display the values of the form. I've mocked up a quick and dirty module so you can see a couple different approaches:

function mymodule_menu(){

  $items = array();

  $items['sample_form'] = array(
     'title' => t('Sample Form'),
     'access arguments' => array('access content'),
     'page callback' => 'drupal_get_form',
     'page arguments' => array('mymodule_sample_form'),
  );

  $items['sample_form/success/%'] = array(
     'title' => t('Sample Form | Success'),
     'access arguments' => array('access content'),
     'page callback' => 'mymodules_sample_form_success',
     'page arguments' => array(2), //pass value of field_two to callback function
  );

  return $items;
}

function mymodule_sample_form($form, &$form_state){

    $form['field_one'] = array(
      '#type' => 'textfield',
      '#title' => t('Field One'),
    );

    $form['field_two'] = array(
      '#type' => 'textfield',
      '#title' => t('Field Two'),
    );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );

    return $form;
}


function mymodule_sample_form_submit($form, &$form_state){

  //This is where you process and store submitted values.
  //Lets use variable_set to store the value
  variable_set('mymodule_field_one', $form_state['values']['field_one']);

  //Ok, lets now redirect the user to the success page and pass field_two as an argument
  $form_state['redirect'] = url('sample_form/success/'.$form_state['values']['field_two'], array('absolute'=>true));
}


function mymodules_sample_form_success($field_two){

  $output = 'Value for field one: '. variable_get('mymodule_field_one').'<br />';
  $output .= 'Value for field two: '. $field_two.'<br />';

  //Lets display the form again, rebuilt.
  $output .= drupal_render(drupal_get_form('mymodule_sample_form'));

  return $output;
}
| improve this answer | |
0

You need a submit handler function. Just add a _submit to the end of your form function


function stock_quotes_get_form_submit($form, &$form_state){
  $symbols = $form_state['values']['symbol'];
  return "You submitted this symbols: " . $symbols;
}

| improve this answer | |
  • Form submission handlers don't return any value. – kiamlaluno Dec 11 '11 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.