Search code examples
phpdrupaldrupal-webform

Pass variables from one page to a webform in drupal


I'm trying to pass one variable from different product pages to a webform page. Each product page has a session variable like $_SESSION["product_name"] = "Product1";. Similarly, other product pages have $_SESSION["product_name"] = "Product2"; $_SESSION["product_name"] = "Product3";

My question is how to pass these variables to a different webform page. I'm using "webform" module, what I want to accomplish is there should be a <select> tag, and there are options like

<select>
  <option value="<?php echo $_SESSION["product_name"]; ?>" selected><? php echo $_SESSION["product_name"]; ?></option>
  <option value="Product2">Product2</option>
  <option value="Product3">Product3</option>
  <option value="Product4">Product4</option>
</select>

The problem is there is no place for me to place these codes, is there any way around this or is there any module I can use for this issue?


Solution

  • You can create a custom module and then implement hook_form__alter()

    You would need to find the IDs of the webforms you want to target.

    Then:

    function mymodule_form_webform1_alter(&$form, &$form_state) {
        array_unshift($form['products']['#options'], array($_SESSION["product_name"] => $_SESSION["product_name"]));
    }
    

    or similar