0

In Wordpress, I try to write o cookie into a hidden field. I have a cookie:

   if(isset($_GET['ecselis']))
     {
     $cookie_name = "ecselis";
     $cookie_value = $_GET['ecselis'];
     setcookie($cookie_name, $cookie_value, time() + (86400 * 30),   "/");
     $_SESSION["ecselis"] = $cookie_value;
     }

   else if(isset($_COOKIE['ecselis'])) {
      $_SESSION["ecselis"] = $_COOKIE['ecselis'];
     }

   else
     {

     }

This working fine. But I dont realy know how should I write it into a hidden field

<input id="ecselis_field" name="ecselis_field" type="hidden" value="" />

I tried

<input id="ecselis_field" name="ecselis_field" type="hidden" value="$.cookie('ecselis')" />

but it doesn't work at all.

Lukas
  • 53
  • 5
  • `$.cookie` is js library. so without loading library you can't use it. also, this will not work, even if you loaded library: `value="$.cookie('ecselis')"` – Samvel Aleqsanyan Feb 20 '18 at 14:08

1 Answers1

2

Here is some solution for you, if you want to use js/jquery ( function for getting cookie getCookie() used from this answer ):

function getCookie(name) {
    var value = "; " + document.cookie;
    var parts = value.split("; " + name + "=");
    if (parts.length == 2) return parts.pop().split(";").shift();
}

jQuery(document).ready(function ($) {
    $('#ecselis_field').val(getCookie('ecselis'));
})

Add this code into some .js file of your theme/child theme or create your own one.

Tested and works.

Samvel Aleqsanyan
  • 2,812
  • 4
  • 20
  • 28