Cookies on PHP – Dropdown or Button Forms

Create Select Button or Dropdown and save with php cookies

How do cookies work?


Create cookies with PHP

Use setcookie to create a cookie with PHP. This cookie will expire after 30 days. Using “/”, cookie is available in all website (otherwise, select the directory you prefer).


This setting will create a global cookie for the entire domain.
If you have subdomains configured, each subdomain will create a different cookie, very useful when you have a site for multiple countries or multi languages

If you want a configuration only for a sub directory or sub folder you should put simple indication in the url path



Use $_COOKIE to retrieve a cookie with PHP. Once the cookies have been set, they can be accessed on the next page load.



Actually, there is not a way to update a cookie. Just set (again) this cookie using setcookie.



Actually, there is not a way to directly delete a cookie. Just use setcookie with expiration date in the past, to trigger the removal mechanism in your browser.


What is the difference between Create and Update ?

In Create and in Update > on line number 2

  • At this point it is important to clarify that the cookie can be updated by an on-demand method, for example buttons, dropdown, checkbox and many other options.


How to submit a cookie update?

One of the options are the variables

And add a dropdown form or with select buttons that when sending data indicate a new value on the variable $varcookie_with_php

Which will be replaced by the use of the variable: $cookie_value



Create a Buttons menu and setting PHP cookie



We will create some variables

While this is a simple method to create and modify a cookie in order to memorize an action or add functionality to a website, it has a disadvantage.

After clicking the button, you need to reload the page or navigate to another page so that the cookie goes into action by sending the information stored in it.

This is why the following code allows 3 actions

A) If the cookie does not exist ….
B) If the cookie is created or modified the variable will deliver the value of the chosen selection.
C) After navigating to the next page, the variable delivers the value served by the cookie


Finally we get the variables, and with a little imagination we will get an endless number of options to apply them





Leave a Reply

Your email address will not be published. Required fields are marked *
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">