Jump to content

Set cookie value from image click


Recommended Posts

Hi all,

 

I am trying to set a custom cookie variable value from an input image type in my tpl.

 

But the value isn't being updated...I don't have any errors in debug mode.

 

Here's the tpl:

<div id="_desktop_daynight">
  {if $is_night eq 1}
    <form method="post">
    <input type="image" name="submit_day" value="Submit" src="/prestashop_1.7.0.3/img/cms/night.png" width="22px" height="22px"/>
    </form>
  {else}
    <form method="post">
    <input type="image" name="submit_night" value="Submit" src="/prestashop_1.7.0.3/img/cms/day.png" width="22px" height="22px"/>
    </form>
  {/if}
</div>

This is handled in the postProcess of my PHP file:

public function postProcess()
{
  if (Tools::isSubmit('submit_night')) {

    global $cookie;
    $context = Context::getContext();
    $context->cookie->__set($is_night,1);
    $cookie->write();
  }
  else if (Tools::isSubmit('submit_day')) {

    global $cookie;
    $context = Context::getContext();
    $context->cookie->__set($is_night,0); 
    $cookie->write();
  }
}

Any help appreciated.

 

Thanks!!

 

Simon

Link to comment
Share on other sites

Could this be because of changes in 1.7? And because of the widget interface?

class Se_Daynight extends Module implements WidgetInterface
{
	public function postProcess()
	{
		if (Tools::isSubmit('submit_night')) {
			
			global $cookie;
			$context = Context::getContext();
			$context->cookie->__set($is_night,1);
			$cookie->write();
			
			$is_working = 1;
		}
		if (Tools::isSubmit('submit_day')) {
			
			global $cookie;
			$context = Context::getContext();
			$context->cookie->__set($is_night,0);			
			$cookie->write();
			
			$is_working = 1;
		}
	}
Link to comment
Share on other sites

I didn't get this working with a form. But I managed to get it working using jQuery - using the jQuery cookie plugin which i uploaded and referenced in the tpl file.

 

Didn't need any variables in my PHP file. Just the tpl:

 

{literal}
<script src="http://localhost/prestashop_1.7.0.3/js/jquery/jquery-1.11.0.min.js"></script>
<script src="http://localhost/prestashop_1.7.0.3/js/jquery/ui/jquery-ui.min.js"></script>
<script src="http://localhost/prestashop_1.7.0.3/js/jquery/plugins/js-cookie/jscookie.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$(".change  a").on('click',function(){
		var daytype = Cookies.get('is_night');
		if(daytype == 0) {
			Cookies.set('is_night', 1, {expires: null});
		}
		else {
			Cookies.set('is_night', 0, {expires: null});
		}
		location.reload();
	});
	$(function(){
		var daytype = Cookies.get('is_night');
		
		if(daytype == 1) {
			var file_name = "night.png"
		}
		else {
			var file_name = "day2.png"
		}
		
		var file_path = "/prestashop_1.7.0.3/modules/se_daynight/img/";
		var new_source_for_image = file_path + file_name;
		$("#day_image").attr("src", new_source_for_image);
	});
});
</script>
{/literal}

<div id="nav_daynight">
		<div class="change">
			<a href="#"><img id="day_image" src="" width="37px" height="37px"></a>
		</div>		
</div>

On clicking the a href from the class "change" I can check and set the cookie variable using Cookies.set('name','value');

 

I then displayed the image i wanted based on the cookie by setting the 'src' attribute of the img id.

 

I guess the jscookie.js plugin should be added in the header hook...but couldn't get this working.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...