My users fill out a registration form (with their emails, and some options). I calculate with ajax from these options a price. I want to redirect them to an other site, so they can pay there the price.

With form_alter I add some hidden fields (components) to my webform (like PaymentRequestId and Payment Status). And I set a custom submit function to the form, as I want to redirect the users.

But I have some difficulties: first I tried to generate and set the PaymentRequestId in my custom submit function, but I was not able to save the value into the formfield of the submission there. I already asked it here, how to do that, but I could not achieve it. So for a suggestion, I moved these into a hook_webform_submission_presave function. Now, I was able to save my PaymentRequestId (HURRAY!).

But I also need this ID in my custom submit function, as the URL (where I need to redirect my users) is generated from this. But neither the $form, nor the $form_state in my custom submit function does not contains the value of the PaymentRequestId. I do not understand why, as when I watch the Submission on the admin interface, the PaymentRequestId is there.

Do you have any ideas/workarounds for my problem? Thanks a lot, for all your efforts!

  • Do you use ajax to submit your form? If so, then you might need to redirect the user in a custom ajax submit callback. – Ismini Oct 4 '17 at 8:29
  • @Ismini: no, I do not use ajax to submit. – Koli Oct 6 '17 at 8:34

First of all you need to make sure that your ajax return your results to the form so as you can have the value on your custom form submit. Maybe one more hidden field with the new value would be the solution. So in your custom ajax callback you need to return $form['new_value_field'];

After that, in your custom submit function you should get the new value and you can save your form in there. $form_state->setValue('new_field', 'new_value');. Even though I'm not sure if you need to set the value again as you already did it with ajax earlier.

Finally, you can redirect the user in your custom submit function by using $form_state->setRedirect('machine_name');

  • Sorry @Ismini, but you misunderstood my question. Most of what you explain was already working. The missing part was to set the value. To your suggestion ($form_state->setValue) I get Fatal error: Call to a member function setValue() on array error message. – Koli Oct 7 '17 at 21:25

I did not find a way with the hook_webform_submission_presave function, but finally I found a solution with the help of this answer to an other question, to save values into the webform submission in my_custom_submit function. Here is the part of my code from my_custom_submit function, which updates hidden field:

module_load_include('inc', 'webform', 'includes/webform.submissions');
$node = node_load($form_state['storage']['details']['nid']);
$submission = webform_get_submission($node->nid, $form_state['storage']['details']['sid']);
foreach ($form['#node']->webform['components'] as $key => $component) {
  if ( $component['form_key'] == 'my_unique_id' ){
    $submission->data[$key][0] = $my_uniqe_key;
webform_submission_update($node, $submission);

I tried all your suggestions, but non of them worked for me. Thanks for all your efforts!

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.