0

I want to connect my HTML code to PHP that when you click on a specific button in a message, do these: 1- set a cookie that don't show it again. 2- hide messages with CSS. And when user come back to my site read cookie and don't show message.

1
  • 2
    This can be done without PHP. Use local storage for that. If you need to trigger PHP script, then google for Ajax request Commented Oct 13, 2021 at 17:39

1 Answer 1

1

Cookies are part of the HTTP requests. When you request a URL it can respond with set-cookie header. The browser read this and changes the cookies.

So, If you want to change a cookie, this must be done by PHP.

Let's have a button.

<a href='dontshowagain.php'>Dont show again</a>

And, PHP code,

setCookie("hideAnnounce", "1"); // add the cookie to response
header("location: /index.php"); // add redirection to response

Now, index.php can read the cookie.

<?php if(!isset($_COOKIE['hideAnnounce'])){ ?>
   <div>announcement</div>
<?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.