3

I'm using webform for submission and trying to simulate GET parameters (though by default it uses POST).

Here's my code implementing hook_form_alter():

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $nid = 2;
  if ($form_id == 'webform_client_form_' . $nid) {
    $option['query'] = array(
        'name' => $form_state['values']['name'], 
        'email' => $form_state['values']['email'], 
        'gender' => $form_state['values']['gender'], 
        'birthday' => $form_state['values']['birthday'], 
        );
    $form['#action'] = url('path/to/myViewsPage', $option);
  }

Above code should take me to

'path/to/myViewsPage?name=tom&email=tom@gmail.com&gender=male&birthday=12/21/2013'

But I got following error:

Undefined index: values in mymodule_form_alter()

Could someone tell me where to define the $form_state['values']? Isn't it predefined by Webform?

0
1

In your form_alter() you should ADD a new submission handler.

function mymodule_form_alter($form, &$form_state, $form_id) {
 $nid = 2;
  if ($form_id == 'webform_client_form_' . $nid) {
    $form['#submit'][] = 'your_submit_handler';
  }
 }

In your_submit_handler() these values will be available

function your_submit_handler(&$form, &$form_state) {
   $option['query'] = array(
    'name' => $form_state['values']['name'], 
    'email' => $form_state['values']['email'], 
    'gender' => $form_state['values']['gender'], 
    'birthday' => $form_state['values']['birthday'], 
   );
   $url = url('path/to/myViewsPage', $option);// (valid path or whatever)
   $form_state['redirect'] = $url;
}

You dont care about the webform node being saved you only need to redirect, right? This will run alongside with yout default webform submit handler.

2
  • Thank U, this works better than drupal_goto(). This has a nicer organizing and help me understand a lot, thanks.
    – movila
    Mar 31 '13 at 16:39
  • Glad it helped , also don't forget to accept an answer or vote up for the ones that helped you out! Yeah drupal_goto() and changing $form['#action'] is a bad idea in form-handling in general..
    – Pan Chrono
    Apr 1 '13 at 8:51
1

It's a bit hackish, but I think it would it be better if you set the path to your View in $form['#action'] and post the form directly to your View. Then on your Views page you grab the posted values from the form, in a contextual filter if you need that. In that way you also completely bypass the submission handling and nothing is saved.

2
  • Thank you, this also works. I use GET only for the purpose of bookmark-able. On an unrelated subject, is there always a Views implementation that substitutes a customized SQL query?
    – movila
    Mar 21 '13 at 6:43
  • I would say mostly, but not always.
    – enzipher
    Mar 21 '13 at 7:10
1
//  echo"<pre>"; print_r($form_state); die;
/*** here you check all values of webform which is submitted ***/

$fname = $form_state['input']['submitted']['Your_field_ID'];
1
  • Yes, this is true. But seems not what I'm asking.
    – movila
    Mar 31 '13 at 16:36
0

Webform has its own hook to access the submit and all. You could use hook_webform_submission_presave().

Modify a Webform submission, prior to saving it in the database.

Parameters

$node: The Webform node on which this submission was made.

$submission: The Webform submission that is about to be saved to the database.

See All Webform Module Hooks.

3
  • I don't want to save the submission data, just a redirection will be fine. Is there a way to prevent data being saved in webform database?
    – movila
    Mar 21 '13 at 5:39
  • Use the hook_webform_submission_presave hook and empty the form state.
    – niksmac
    Mar 21 '13 at 5:49
  • Thank you. Actually I solved my problem by using drupal_goto() which (seems to me) served the purpose of redirection.
    – movila
    Mar 21 '13 at 6:23
0

Try this:

 $form_state = form_state_defaults();
 $form_build_id = $_POST['form_build_id'];
 // Get the form from the cache.
 $form = form_get_cache($form_build_id, $form_state);

or by providing the form id:

credits: How do I load the $form_state for a form loaded with drupal_get_form?

// Get the form.
$form = drupal_get_form('my_form_id');
// Get the default form state.
$form_state = form_state_defaults();
// Get the form and form state from the cache for the form you just got.
form_get_cache($form['#build_id'], $form_state);

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.