PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

gform pre render and cookies

  1. Alex
    Member

    I am trying to make an email route based on whether a cookie is satisfied. However, it's harder than I thought. What trick am I missing? Here's what I have:

    cookie is $_COOKIE["src"] and will have nothing if not triggered. To route the email I know that I need a selection enabled to use as conditional logic. I want to create this selection field and CSS it as display:none.

    I have been able to get the right code except here the function does not know that the cookie is set:

    if($_COOKIE["src"]){
    $items[] = array("value" => "gsm", "text" => "gsm");
    } else {
    $items[] = array("value" => "", "text" => "");
    }

    this is the entire code:

    function my_custom_population_function($form){
    $items = array();
    if($_COOKIE["src"]){
    $items[] = array("value" => "gsm", "text" => "gsm");
    } else {
    $items[] = array("value" => "", "text" => "");
    }
    foreach($form["fields"] as &$field)
    if($field["id"] == 8){
    $field["choices"] = $items;
    }
    return $form;
    }

    also, i will want to share this amongst all forms but the input number will not always be 8. any way around duplicate code?

    Posted 12 years ago on Monday January 16, 2012 | Permalink
  2. So the issue is the $_COOKIE global does not have a record of your cookie? Are you sure the cookie is being set correctly? I did not spot any issues with your code.

    To specify a different field ID per form, you could set up a simple array which would contain the form ID as the key and the field ID as the value:

    [php]
    $field_ids = array('1' => '8', '2' => '14');

    In this example, form ID points to field ID 8, form ID 2 points to field ID 14. Then you'd redo your field ID conditional like so:

    [php]
    if($field["id"] == $field_ids[$form['id']]){
        $field["choices"] = $items;
    }
    Posted 12 years ago on Monday January 16, 2012 | Permalink
  3. Alex
    Member

    there's definitely a record of the cookie as i can generate it via this line:

    $items[] = array("value" => "gsm", "text" => "gsm");

    I can use jQuery to output the value with this code fine:

    <?php if($_COOKIE["src"]){ ?>
    <script type="text/javascript">
    var src = <?php echo $_COOKIE["src"];?>;
    </script><?php } ?>

    <script type="text/javascript">if(src){document.write(src);}</script>

    Posted 12 years ago on Monday January 16, 2012 | Permalink
  4. Try using the "isset()" function.

    [php]
    if(isset($_COOKIE['src']){
       ...
    }
    Posted 12 years ago on Monday January 16, 2012 | Permalink
  5. Alex
    Member

    tried it to no avail :(

    Posted 12 years ago on Monday January 16, 2012 | Permalink
  6. Alex
    Member

    an update: i've been able to generate the input via jQuery. it's added into the selection which is display:none and is successfully added as a notification to the form.

    now, so I want to now get "gsm" into the routing option like so:
    http://dl.dropbox.com/u/14646537/image.png

    and i found the Gform_admin_pre_render function here:
    http://www.gravityhelp.com/documentation/page/Gform_admin_pre_render

    this is the code i've made based on that link, but it doesn't seem to work (based on this being input 8 of form 2 and adding it to both functions.php and entry_detail.php):

    add_filter("gform_admin_pre_render_2", populate_dropdown);
    function populate_dropdown($form){
    $items = array();

    $items[] = array("value" => "gsm", "text" => "gsm");
    foreach($form["fields"] as &$field)
    if($field["id"] == 8){
    $field["choices"] = $items;
    }
    return $form;
    }

    Posted 12 years ago on Tuesday January 17, 2012 | Permalink
  7. Alex
    Member

    nobody know how to do this?

    Posted 12 years ago on Thursday January 19, 2012 | Permalink
  8. Jive Software
    Member

    I am trying to do something similar...

    I think the issue is the pre render hook happens after the http headers are sent... you have to run your logic in the wordpress init hook... But then you have have the challange of getting the correct wp and gf data before the post has even been defined...

    Posted 11 years ago on Friday November 2, 2012 | Permalink
  9. Jive Software
    Member

    Ignore my comment above... It looks like you need to set the domain on the cookie...
    setcookie("test", 1, time()+3600, "/", str_replace('http://www','',get_bloginfo('url')) );

    Posted 11 years ago on Friday November 2, 2012 | Permalink