Hi all,

I have seen this question asked a number of times, without ever receiving a definitive answer on whether or not this is even possible.

Is it possible to get at the user-submitted values from the "Confirmation message or redirect URL" box?
And if so, how?

Neither the field key nor the label seem to work -- how do we access the user-submitted values as
PHP variables? $_GET, $_POST, and $_REQUEST only seem to contain limited information.

I would like to send a confirmation email from this box, but I can't get the user's submitted email address!!!

Thanks!

Comments

quicksketch’s picture

It's not easily possible to get at any of the submitted information in the confirmation message. The *only* thing you have to work with is the submission ID (which is in the URL of the page if you're using just a confirmation message).

If you're using the 2.x version (which I'd recommend trying out), you can load the submitted values with the following bit of code:

$nid = arg(1);
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
empowerment’s picture

Thanks! How does upgrading to 2.x beta affect existing forms?

quicksketch’s picture

There are changes in the theming code mostly that will affect sites that have themed the outgoing e-mails. The details of the changes are listed in CHANGELOG.txt.

If you're using webform in a "basic" capacity (not theming or using an extensive amount of form submission or validation code), then you shouldn't have to do any work when upgrading.

empowerment’s picture

Not sure if it makes sense to continue this here (please do advise on that), but I'm having trouble
upgrading to 2.x.

Whether upgrading to 5.x-2.0-beta1 or 5.x-2.x-dev, I get this error:
"The update process was aborted prematurely while running update #13 in webform.module"

And I've just tried going to 1.9 but I get the same error.

Not sure what to look for in the watchdog table, and not really up to speed on SQL, so any advice would
be much appreciated.

Anonymous’s picture

I have added your snippet to the Confirmation message or redirect URL.
However, following error is returned:

Fatal error: Call to undefined function webform_get_submission()

I am using version 5.x-2.0-beta2.

Any ideas?

quicksketch’s picture

Oh, I forgot that webform_get_submission() is in an include file. Try this instead:

include_once(drupal_get_path('module', 'webform') .'/webform_submissions.inc');
$submission = webform_get_submission($nid, $sid);
Anonymous’s picture

Thanks, I should have found that one myself!

It works now but referencing some value could be broken easily when new fields are added:

$submission['data'][4]['value'][0]

Is there any other way to retrieve the values by fieldname or something?

Thanks, Joep

quicksketch’s picture

The component ID (4 in your example) won't change even as new components are added or even if the component name itself is changed. I know it's a bit unfortunate that numeric indexes are used, but it's one of the reasons why Webform is no CCK. Because all data is stored in a single table, the numeric indexes are used to get speed out of an otherwise inefficient database structure.

Anonymous’s picture

Ok, that makes sense.
That will work and I will not have to worry updating fieldnames or changing the order.

Thanks for your reply.

quicksketch’s picture

Status: Active » Closed (fixed)
joshuah7’s picture

Priority: Normal » Minor

Since the listed code above was helpful but incomplete (at least for PHP 4), here is a full code sample:

include_once(drupal_get_path('module', 'webform') .'/webform_submissions.inc');
$nid = arg(1);
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
// Uncomment the following to see all the form values.
//print_r($submission);
$my_form_value = $submission->data[2]['value'][0];

Versions:
Webform 5.x-2.3
Drupal 5.12
PHP 4

najibx’s picture

how about D6? just tried #11 & #6, not working.

myDRU’s picture

In D6, #11 does work without any change, I just tried it.

andrewfn’s picture

Here is a suggestion with improved security. It prevents the submission being easily read by other users. I have only tested it on Drupal 6.

<?php
include_once(drupal_get_path('module', 'webform') .'/webform_submissions.inc');
$nid = arg(1); // need to hard-code nid if this is a custom page
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
// if someone else is trying to snoop on the submission from a different IP, give them a 404
if ($submission->remote_addr != ip_address()) {
  drupal_not_found();
  module_invoke_all('exit');
  exit;
}
$my_form_value = $submission->data[2]['value'][0];
?>

<p>Thank you for submitting a form</h3>
<p>Field 2: <?php print $my_form_value; ?></p>
shanep’s picture

This looks great, any way to get it to work in D5? Confirmation with no snoopers is awesome, thanks!

andrewfn’s picture

It probably does work in D5. I just haven't tested it.

Lubas’s picture

It doesn't work :(
I just get this:
remote_addr != ip_address()) { drupal_not_found(); module_invoke_all('exit'); exit; } $my_form_value = $submission->data[2]['value'][0]; ?>

Thank you for submitting a form

Field 2:

What do I do wrong? I just have to put it in the textarea 'confirmation message or redirection URL', isn't it?
And wich format should I use? Messaging plain text?
Or is there something i have to edit in the code?

Thanks!
Lukas

andrewfn’s picture

Do you have php enabled as the input format of the confirmation form?

Lubas’s picture

I don't really know sure, but I don't think so.
How can I enable this?

edit: I found out how to enable this, but it still doesn't work, I get the same piece of code again.
What should I do, please?

mpaler’s picture

Here's another level of security that will permit access to the confirm page for a preset period of time:

same code as #14 but with an added time check:

if ( ($submission->remote_addr != ip_address() ) || ( (time() - $submission->submitted) > 60 ) ) {

The "60" in the condition is seconds.

Anandyrh’s picture

Hi,

Where do i need to paste this code?

<?php
include_once(drupal_get_path('module', 'webform') .'/webform_submissions.inc');
$nid = arg(1); // need to hard-code nid if this is a custom page
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
// if someone else is trying to snoop on the submission from a different IP, give them a 404
if ($submission->remote_addr != ip_address()) {
  drupal_not_found();
  module_invoke_all('exit');
  exit;
}
$my_form_value = $submission->data[2]['value'][0];
?>

<p>Thank you for submitting a form</h3>
<p>Field 2: <?php print $my_form_value; ?></p>
andrewfn’s picture

It goes on the confirmation page (assuming you are using a separate page for confirmation).

Anandyrh’s picture

Thanks a lot man!

chrispooh’s picture

Hello, is it possible to have a back link to edit the form if the user would like to change some datas?

attiks’s picture

Code is workiing, but I ran into a problem with cron, so I had to remove the line 'module_invoke_all('exit');
', I guess it has something to do with cron indexing pages for search.

Michsk’s picture

how could i use this in a loop?

I got a x amount of questions, all awnsers got a score 0 , 1 , 2 or 3. I would need the total score for all awnser...

//EDIT: let me rephrase the question. How can i put all the points in one array so i can use array_sum().

devkinetic’s picture

here is the view of the block I used for a site. It adds each component's question and also converts things like "not_sure" to the actual rendering of "I'm not really sure" that one would expect displayed.

module_load_include('inc', 'webform', 'includes/webform_submissions');
        $nid = arg(1); // need to hard-code nid if this is a custom page
        $sid = $_GET['sid'];
        $submission = webform_get_submission($nid, $sid);
        // if someone else is trying to snoop on the submission from a different IP OR after to long, give them a 404
        if ($submission->remote_addr != ip_address()) {
        //if ( ($submission->remote_addr != ip_address() ) || ( (time() - $submission->submitted) > 120 ) ) {  // comment this line for development
          drupal_not_found();
          module_invoke_all('exit');
          exit;
        }
        foreach ($submission->data as $id => $item) {
          $component = db_query('SELECT name, type, extra FROM webform_component WHERE cid = %d AND nid = %d', $id, $nid);
          while ($obj = db_fetch_object($component)) {
            $submission->data[$id]['name'] = $obj->name;
            $submission->data[$id]['type'] = $obj->type;
            $options = unserialize($obj->extra);
            $options = explode("\n",$options['items']);
          }
          foreach ($options as $option) {
            $bla = explode("|", $option);
            $submission->data[$id]['values'][$bla[0]] = $bla[1];
          }
          switch ($submission->data[$id]['type']) {
            case 'select':
              foreach ($submission->data[$id]['value'] as $ident => $item) {
                $submission->data[$id]['value'][$ident] = $submission->data[$id]['values'][$item];
              }
              break;
            case 'hidden':
              unset($submission->data[$id]);
              break;
            
          }
        }
        $content = theme('form_submission_block', $submission->data);
        $block['subject'] = '';
        $block['content'] = $content;

dmidkiff’s picture

Version: 5.x-1.7 » 6.x-3.0
Status: Closed (fixed) » Active

I have added this code to both the confirmation page and as a redirect to a custom page but I get the same error.

Fatal error: Call to undefined function webform_get_submission() in my_path/includes/common.inc(1699) : eval()'d code on line 5

quicksketch’s picture

Version: 6.x-3.0 » 5.x-1.7
Status: Active » Closed (fixed)

Support for custom code is no longer provided in the Webform issue queue.

matt b’s picture

Version: 5.x-1.7 » 6.x-3.0

for 6.x-3.0 the following code needs to be used as the module include has been moved to a subfolder and renamed, and I've also made use of the modules functions to display the submission.

<?php
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');

$submission = webform_get_submission(arg(1),$_GET['sid']);
if ( $submission->remote_addr != ip_address()  ) {
//if ( ($submission->remote_addr != ip_address() ) || ( (time() - $submission->submitted) > 120 ) ) {  // comment this line for development
  drupal_not_found();
  module_invoke_all('exit');
  exit;
}

$node= node_load(arg(1));
$output = webform_submission_render($node, $submission, NULL, 'html');
return theme('webform_submission_page', $node, $submission, $output, null, null);
?>

Hope this helps, but suggest you don't raise any tickets regarding your code as it appears this is not supported. You probably should consider putting this sort of code into a separate module...

ikeigenwijs’s picture

keeping track of this idea.

chaosmind’s picture

subscribe. the comments in this thread officially saved my butt. t'anks, everyone!

Sinan Erdem’s picture

subscribing..

mbria’s picture

subscribing..

robin van emden’s picture

Category: support » task

Very useful info in this issue queue - enabled me to *quickly* create a simple conditional answer options solution in the webform-form.tpl.php, using a little jQuery magic, might be of interest to some, see:

http://druplicity.com/content/webform-conditional-answer-options

I just might create a module based on this idea, but probably will not find the time.

(ps: issue category wrong, sorry)

woop_light’s picture

Version: 6.x-3.0 » 7.x-3.0-beta2

I am hoping to access form values AND redirect to a Custom URL for the Confirmation Page. It appears the code is equipped to handle that based on the comment "// need to hard-code nid if this is a custom page", but I couldn't get it to work. Hopefully someone might be able to help.

A few (perhaps misguided) questions:
(1) If my Custom URL is a basic page, should the above code be pasted to the to the body of the Basic page, or still as the Confirmation Message?
(2) I see that I must no longer use "arg(1)" and instead use the Node Id. If I'm using Auto Alias, how do I figure out what the Node ID is?
(3) Let's say I learn it's 43. What exactly would I then replace "arg(1)" with? (expecting something along the lines of node/43 or just 43)

I thank everyone for their help on this.

Woop

umesh_chaudhari’s picture

Version: 7.x-3.0-beta2 » 6.x-3.11

i am putting following content into the confirmation message box of the webform to get the form values but i am not able to fetch any form values.what can be the possible problem.please help.

<?php
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$nid = arg(1); // need to hard-code nid if this is a custom page
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);

$name = $submission->data[6]['value'][0];
$thanks = $name .
?>

<h2>Thank you <?php print $thanks ?>... Your registration has been sent.</h2>
umesh_chaudhari’s picture

hi #14 worked for me after some modifications.
i had changed the following line

include_once(drupal_get_path('module', 'webform') .'/webform_submissions.inc');

as follows,

include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');

and it worked for me.

jweedman’s picture

Same Here! Worked for me to change the path to just "/webform_submissions.inc"

sbalay’s picture

I tried doing with the same code. It is giving me an error

The website encountered an error while retrieving http://example.com/node/33/done?sid=127. It may be down for maintenance or configured incorrectly.

Is there something should I add to the code?

ressa’s picture

@woop_light: You can access form values AND redirect to a Custom URL by including the nid in your custom redirect URL like this: webform-url/result?first_name=%value[first_name]&last_name=%value[last_name]&nid=%nid
sid is automatically appended, so you don't need to include that.

Use this to get the data in the webform, see what's available and print it:

$nid = $_GET['nid'];
$sid = $_GET['sid'];
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$submission = webform_get_submission($nid, $sid);

// See potential content
print dsm($submission);

// Print for example the remote address
print $submission->remote_addr;

This works for Drupal 7, I don't now if it will work in D6.

ibexy’s picture

This give the error:

Notice: Undefined index: nid in eval() (line 2 of /mywebsite/modules/php/php.module(80) : eval()'d code).

Notice: Trying to get property of non-object in eval() (line 11 of /mywebsite/modules/php/php.module(80) : eval()'d code).

am using Drupal 7

ressa’s picture

You can see under "Token values" if %nid is available, I think it should be, but perhaps it's another issue? I ended up using this shorter version: webform-url-result/%nid/%sid and getting the values using arg(1) and arg(2).

doublejosh’s picture

It should be noted that the $data[INDEX] is from the component created index not it's sort order... which is good.
My code ended up looking like...

// Collect answers via submission lookup.
$nid = htmlspecialchars($_GET['nid']);
$sid = htmlspecialchars($_GET['sid']);
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$submission = webform_get_submission($nid, $sid);
// NOTE: Webform component submissions within $data are only indexed by number.
$answers = array(
  'QUESTION1' => $submission['data'][5],
  'QUESTION2' => $submission['data'][3],
  'QUESTION3' => $submission['data'][4],
);
// Output values.
foreach ($answers as $a) {
  echo $a . '<br />';
}

My use case is processing values into a composite "score" in place of that output.

Manick’s picture

Title: Accessing form values from confirmation message box » Creating users automatically with help of webforms
Version: 6.x-3.11 » 7.x-3.19
Component: User interface » Code
Assigned: Unassigned » Manick
Priority: Minor » Critical
Status: Closed (fixed) » Active

Can Anyone help me in this? I need to create users as and when an anonymous user fills webforms. I did use some forum material to create new users - using User_save function. But what happened was when i tried to fill the form with same user name got Exception error. Ofcourse, as 2 users should not have same username.

In addition to that, if the user is already logged in and if he tries to submit, that will be an error as the account already exits. Is there any way that i can go about in avoiding errors. I tried using webform rules module. But there wasn't much of explanation/ examples from which one can learn

devkinetic’s picture

Title: Creating users automatically with help of webforms » Accessing form values from confirmation message box
Version: 7.x-3.19 » 6.x-3.11
Component: Code » User interface
Assigned: Manick » Unassigned
Status: Active » Closed (fixed)

@Manick Please keep this thread focused on the original issue.

Your question is better suited for submission to the drupal stack exchange, as it involves writing custom code, and is not an issue with the intended functionality of the webform module. One thing right off the bat is you need to add checks to see if the username exists, if the user is logged in already, etc.

cats’s picture

Version: 6.x-3.11 » 6.x-3.19
Component: User interface » Code
Priority: Critical » Major
Status: Closed (fixed) » Active

$_GET['sid'] works for a separate confirmation page, but how would I access the SID if I'm redirecting back to the original form?

quicksketch’s picture

Issue summary: View changes
Status: Active » Fixed

You can use the %sid token in your redirect URL to include it in the query string of your redirect URLs in the 3.x version. In 4.x you can use [submission:sid].

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

matness123’s picture

Hi everyone, I found this post helpfull.
With Drupal 7 I am unable to get data in the submission array with the provided code :
$submission->data[1]['value'][0], it returns nothing
Simply remove the ['value'] and it's now working fine !
Using webforms 4.x and Drupal 7.x
Thanks !

d8v15’s picture

using drupal7 webform 4 and this code doesnt work.

I show the confirmation message in a jquery pop message (reload current page). My goal is simply to get a value from the form and show in the "confirmation message" box.

after including the scripts in "confirmation message" as above, calling arg(1) returns "ajax". As well, there is no value sid in the get string. With this i cannot retrieve form values (webform_get_submission returns nothing).

Please advise.