Is there a simple way to let webform to show a confirmation page, with a user choices summary, after sending the form?
I know webform can redirect POST arrays, but it registers the submitted form, and i don't want this till user has confirmed his choises.

I'm a new drupal user, so i've found a solution which seems to work but it is probably too rough, maybe buggy, i don't know. Any suggestion, help or comment is appreciated.
Samuele

Put this code in the "Addiotional Validation" field of your form (_my_submission_format create the summary message and is modeled after webform_submission_format webform function):


if ($_POST['to_hidden']=='1' || form_get_errors()) return;
form_set_error('', t('Confirm your submission.'));
t('Riepilogo dati:');
$output .= nl2br(_my_submission_format("", $form_values['submitted_tree'], $node));

$myform=drupal_get_form('tohidden');
$cgtoken=drupal_get_token('tohidden');
$nwtoken=$form_values['form_token'];
$myform = str_replace($cgtoken,$nwtoken,$myform);
$myform = str_replace('tohidden',$form_id,$myform);
$output.=$myform;
print theme('page',$output);
exit;

function _my_submission_format($key, $value, $node, $indent = "") {
    // First check for component-level themes
    $themed_output = theme("webform_mail_". $node->webformcomponents[$key]['type'], $value, $node->webformcomponents[$key]);
    if ($themed_output) {
      // Indent the output and add to message
      $message .= $indent;
      $themed_output = rtrim($themed_output, "\n");
      $message .= str_replace("\n", "\n". $indent, $themed_output);
      $message .= "\n";
    }
    // Generic output for single values
    elseif (!is_array($value)) {
      // Note that newlines cannot be preceeded by spaces to display properly in some clients
      if ($node->webformcomponents[$key]['name']) {
        // if text is more than 60 characters, put it on a new line with space after
        $long = (strlen($indent . $node->webformcomponents[$key]['name'] . $value)) > 60;
        $message .= $indent.$node->webformcomponents[$key]['name'] .":". (empty($value) ? "\n" : ($long ? "\n$value\n\n" : " $value\n"));
      }
    }
    // Else use a generic output for arrays
    else {
      $message .=  $node->webformcomponents[$key]['name'] .":\n";
      foreach ($value as $k => $v) {
        $message .= _my_submission_format($k, $v, $node, $indent ."  ");
      }
    }
    return ($message);
}


function tohidden(){
 global $form_values,$_POST;
 $tform=convertform($form_values);
foreach ($tform as $field_id => $value) {
 $form['submitted'][$field_id]=$value;
}
 $form['submitbutton'] = array(
  '#type' => 'submit',
  '#value' => t('Confirm'),
);
 $form['to_hidden'] = array(
       '#type' => 'hidden',
       '#value' => true
     );
    
 return $form;
}

function convertform ($sform){
 foreach ($sform as $field_id => $value) {
   if (is_array($value)) {
     $cform[$field_id] = array(
       '#tree' => TRUE
     ); 
     $sform2=convertform($value);
     foreach ($sform2 as $field_id2 => $value2) {
      $cform[$field_id][$field_id2]=$value2;
     }
   } else {
     $cform[$field_id] = array(
       '#type' => 'hidden',
       '#value' => $value
     );
   }
 }
return $cform;
}

Comments

harro’s picture

Hi there - this is definitely a very useful piece of code! For me ther eis one snag, I am still on Drupal 4.7.x and this gives me the following error:

warning: Missing argument 2 for drupal_get_form() in /.../public_html/includes/form.inc on line 61

I see this is a drupal 5 hack/addon, but I was wondering whether you could suggest how to solve this error message for the previous version? There are still many sites that run on the 'old' version, so many people would be happy to use the fix... :)

Thank you!

Harro

samuelet’s picture

I'm glad that it's useful :).

About 4.7, try to give an empty array as drupal_get_form second argument.
Substitute line 6:

...
$myform=drupal_get_form('tohidden');
...

with

...
$form = array();
$myform=drupal_get_form('tohidden',$form);
...

hope it works.

harro’s picture

Thank you Samuelet! The error is gone, wonderful. Now the next 'issue' appears :P

The message at the top of the screen is: "Confirm your submission." But there is no submit button to confirm with...! Is this something that needs to be added manually, or is that an issue that only works with Drupal 5 ?

Bye!

Harro

samuelet’s picture

Unfortunately, that code is for drupal 5.1 .
Probably there is incopatibilty beteween the 5.1 and 4.7 creation form function. You should look at drupal_get_form and tohidden function and try to port them to 4.7.

harro’s picture

thank you, I will have a peek around for info on those functions :)

I'm considering switching to Drupal 5 anyways, although it will probably be Drupal 6 by the time the conversion is complete...

nk_’s picture

thanks for this, it is really something i was looking for.

since i need something specific for my case and i am not a php experienced i have 2 questions, any help would be much appreciated.

1. i found that values of the file field are not displayed, i guess it has something with using 'submitted_tree' or 'submitted' arrays. when i change this to 'submitted' i get long description of the file. i guess i should look in module's fuction for displaying results in admin mode and find a way to display only file name ...
2. i put your code in additional processing as my aim is not confirmation but possibility for user to print previously submitted form so i am looking for option to generate print button to print formatted content.

thanks again.

samuelet’s picture

1) What is the long description? Do you mean the full path? In this case you could use the basename php function to get it.
However, best way is to debug submitted values putting this code at the start of the additional processing code.

echo "<pre>";
var_dump($form_values);
echo "</pre>";

2) In the tohidden funct, instead of :

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

try:

$form['submitbutton'] = array(
  '#type' => 'button',
  '#value' => t('Print'),
  '#attributes' => array('onclick' => 'window.print();return false;')
);

This work only on a browser that supports javascript because there is no printing button in strict xhtml.

nk_’s picture

hi samuele,

thank you very much for reply.

1) sorry, i thought of 'serialized output' something like this: 'a:5:{s:8:"filename";s:10:"some.gif";s:8:"filepath"; .... '
i know that perhaps i shoud use unserialize() function but i don't know how to do that -- how to check if component is file and to unserialze to get only filename :(

2) thanks, i've solved this using javascript.

Sam Roy’s picture

Thanks for this piece of code, it's very useful indeed.

I'm using the redirect URL option for my webform and I need to POST the fields to this new URL. Unfortunatly, the field names are returned "a la Drupal" with field names resembling submitted[xxxxxxxxxx].

My questions is this:

How can we rename or change the value of the name="" attribute of any given form field prior to posting the new hidden form fields to another URL?

:)

"Living la vida digitale"
http://www.samericka.com/
tel.: 514-907-1277
skype: samericka

mkrakowiak’s picture

Samericka, did you figure out how to change the value of the name="" attribute when using Webforms? I'm having the same problem.

Tom-182’s picture

Hi there,

I've tried your code on Drupal 5.1 and Webform 5.x-1.4, and when I click the submit button, the confirmation page only show a "Confirm" button. I can't see everything user have submitted on the previous page.

Hope you can have a solution for this. Thank you.

http://www.geektips.net

shahin.bonakdar’s picture

This is a very useful code and it works beautifully on my system.

However from the confirmation page can there be a button that takes user back to the form for correction of the filled up fields. If so then how can this be done.

samuelet’s picture

Probably It's not the best solution, but it works.
Add this at before the return statement in the "tohidden" function:

$form['backbutton'] = array(
  '#type' => 'button',
  '#value' => 'Back',
  '#attributes' => array('onclick' => 'history.go(-1);return false;')
);

About my code compatibility, I still don't know if works on last webform module, but soon my drupal server will be no more in a production state, and i'll upgrate it for testing.

amariotti’s picture

Anything new on this? It'd be really nice if in the redirect page there were variables available to print on the page. Does anyone know if this is possible?