Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8587907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:41:08+00:00 2026-06-11T22:41:08+00:00

I don’t know anything about cookies or how to set them and I need

  • 0

I don’t know anything about cookies or how to set them and I need some advice. I have two radio buttons. For example if an option is changed from one to another, that option will remain even if the page is refreshed or changed on other pages where this radio buttons exist, and I need to make the cookie setting for this code. Can someone can give me some advice regarding what code should I add to my php?

This is js code:

$(document).ready(function() {
  $('radio[name=radio]').each(function() {
   $(this).click(function() {
    my_function();
    });
   });
});

my_function()
{
   var value_checked = $("input[name='radio']:checked").val();
   $.ajax({
   type: 'POST',
   url: 'page.php',
   data: {'value_checked':value_checked},
   });
}

html code

<form>
    <div id="radio">
        <input type="radio" id="radio1" name="radio" checked="checked" /><label for="radio1">Choice 1</label>
        <input type="radio" id="radio2" name="radio" /><label for="radio2">Choice 2</label>
    </div>
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:41:09+00:00Added an answer on June 11, 2026 at 10:41 pm

    It is important to remember that cookies can only be set before any output is sent to the client on a webpage, because cookies are set as headers, and headers can only be sent prior to any part of the webpage output. Therefore, you need to refresh the page to set a cookie to the value of your radio button.

    At the very top of the php, BEFORE the <!DOCTYPE> html or <html> tag, you need to add something like this:

    <?php
    if(isset($_POST['radio1'])) {
      setcookie('radio1', true, 600, '/');
      setcookie('radio2', false, 600, '/');
    } else if(isset($_POST['radio2'])) {
      setcookie('radio2', true, 600, '/');
      setcookie('radio1', false, 600, '/');
    }
    ?>
    

    The above code will make sure that only one of the cookies is set to true and the other is set to false. The cookies will expire after ten minutes.

    This is after you properly set up the html form so that you can detect that your user has selected a button:

    <form method="POST" action="index.php">
      <div id="radio">
        <input type="radio" id="radio1" name="radio" checked="checked" />
        <label for="radio1">Choice 1</label>
        <input type="radio" id="radio2" name="radio" />
        <label for="radio2">Choice 2</label>
      </div>
    </form>
    

    The PHP Manual page has more information: http://php.net/manual/en/function.setcookie.php

    EDIT: Semantic code changes and fixed the html tags described.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know if the title describes anything about what I'm trying to say but
don't know what happened in my jcrop selection. I think I pressed some keys
Don't they both have to convert to machine code at some point to execute
Don't know a whole lot about streams. Why does the first version work using
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Don't know if it matters, but the msg.d is about 300 rows long. I
Don't know if this has been answered before. Have custom routes to users. If
Don't know much about encryption... Say I'm preparing a SAML request to submit to
Don't know why but I can't find a solution to this. I have 3
Don't know the term for the red validation border, does it have one? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.