0

I use this PHP snippet on the server side after a submission of a webform:

function mymodule_webform_submission_insert($node, $submission) {
 // $node will give you the node id of your webform
 // $submission will give you the id of your data which you inserted in web form
 $sid = $submission->sid ? $submission->sid : FALSE;
 if($sid){
   dsm($sid); // yes, the sid is shown after a submission 
   if(!isset($_SESSION['mymodule'])){
     $_SESSION['mymodule'] = array();
   }
   $_SESSION['mymodule']['webform_id'] = $sid;
   drupal_add_js(array('mymodule' => array('webform_id' => $_SESSION['mymodule']['webform_id'])), 'setting');
 }
}

... and then on the client side I have ths JS snippet:

(function ($) {

     Drupal.behaviors.mymodule = {
    attach: function (context, settings) {

                var sid = Drupal.settings.mymodule.webform_id;
                console.log('the submission ID is: '+ sid);
        }
    };

}(jQuery, Drupal));

.. but the variable sid is "undefined" in the console (after a submission) no matter what I try to do. Can anyone see what I am doing wrong?

3
  • Have you confirmed the if($sid){ block is being entered in your PHP code?
    – Clive
    Commented Apr 5, 2017 at 9:14
  • Yes, I have checked that with a dsm($sid); inside the if statement. That works after every submission.
    – TBJ
    Commented Apr 5, 2017 at 9:16
  • Probably need some more info then, the code you have there is correct on the surface. Check what you have for Drupal.settings.mymodule in the console.
    – Clive
    Commented Apr 5, 2017 at 9:26

1 Answer 1

2

The problem here appears to be the location in which you are calling drupal_add_js(). The way Drupal works with form submissions, is that after processing is complete, it does a redirect to the page again. This way when you click back (or forward) in the browser, you don't get the message asking you if you want to post the data again. You are adding your JS in a processing function, which happens before the redirect, so it is getting lost after the redirect.

You'll need to find a different location to put your call to drupal_add_js(), that comes after the redirect. It should go in the same place where you are loading your script in the first place.

3
  • Thanks for your answer. But after adding the value to a session variable, shouldn't I be able to retrieve it even after a redirect?
    – TBJ
    Commented Apr 7, 2017 at 6:16
  • Hello again... I begin to understand what you mean. Disregard my first comment. I will look into where to put the drupal_add_js().
    – TBJ
    Commented Apr 7, 2017 at 6:58
  • That worked! I moved the drupal_add_js line to my template.php in the hook_preprocess_page function. Thanks again! Upvote and right answer.
    – TBJ
    Commented Apr 7, 2017 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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