Cannot retrieve value on client side 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-&gt;sid ? $submission-&gt;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' =&gt; array('webform_id' =&gt; $_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?
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.