0

I have a checkbox that when I click I want it to set a cookie with a value of an array. But when I click on the checkbox it doesn't set my cookie.

I don't understand why it is not being set. Is there something wrong with how I am creating the array for the value of the cookie?

Here is my code:

 extract($_POST);
 $code = $_POST['code'];
 $total= 100;
 if(isset($save)){
        echo "Saved";
        $cookie = array(
            'name'   => $code,
            'value'  => $total,
    );
    setcookie("cookie",json_encode($cookie),86500);
}

 if(isset($_COOKIE['cookie'])){
        echo "SET";
    }else{
        echo "NOT SET";
    }


<form method='post'>
     <input type="checkbox" name="save" value="1">Save
     <input name='code' />
</form
james
  • 29
  • 1
  • 6
  • If you had proper PHP error reporting enabled, this would in all likelihood give you an error about headers already being sent - because you are creating output already, before you called `setcookie`. Also, not clear where `$code` and `$total` are actually supposed to come from. Guess you probably have more form fields by those names, but you're really supposed to show a proper [mre] when asking here. And `extract` is not a good method to use in such a context, you should go read up on what dangers that can pose. – CBroe Mar 23 '22 at 07:12
  • 1
    @CBroe i showed where $code and $total is coming from. about the headers already being sent error you mentioned, what can i do to fix this and set the cookie? – james Mar 23 '22 at 07:16
  • clicking a checkbox would **NOT** submit the save value by POST. (especially you do not have any ajax) – Ken Lee Mar 23 '22 at 07:16
  • @KenLee then what should I do? – james Mar 23 '22 at 07:17
  • Add a submit button to the form ...? – CBroe Mar 23 '22 at 07:18
  • @CBroe i have one and its the same thing. it doesn't get set – james Mar 23 '22 at 07:18
  • @CBroe i want it such that if a user checks the checkbutton for the save, it saves the information when submitted. – james Mar 23 '22 at 07:19
  • _"i showed where $code and $total is coming from."_ - not really. All we see is `extract($_POST);`, but whether there really _are_ items under those keys in there, we can't tell. _"about the headers already being sent error you mentioned, what can i do to fix this and set the cookie?"_ - well if the _problem_ is that you are creating output before something, then the _solution_ would probably be that you stop doing that ...? – CBroe Mar 23 '22 at 07:19
  • _"it saves the information when submitted"_ - and based on what _does_ this form submit ...? – CBroe Mar 23 '22 at 07:20
  • @CBroe what do u mean creating output before something? $code is the value of the input named code and $total is a fixed value. can you please guide and help on how to solve the problem, maybe give a detailed answer and how it should work and where the error is, that would be great. – james Mar 23 '22 at 07:22
  • @CBroe When the user fills the full information of the form, I didn't include the full code to focus on only the error or problem i am getting. Please do you have anything that can help? – james Mar 23 '22 at 07:24
  • _"what do u mean creating output before something?"_ - https://stackoverflow.com/a/8028987/1427878 – CBroe Mar 23 '22 at 07:25
  • @CBroe can you help solve the question please? – james Mar 23 '22 at 07:27

1 Answers1

1
  1. As mentioned by CBroe, you should avoid using extract (for security).

  2. You should have a submit button (but I suppose you have one but did not include in your code posted, right?)

  3. Please use a time() to calculate the life span of the cookie, so use something like:

setcookie("cookie",json_encode($cookie),time()+3600);
  1. use a header redirection to reload your script upon setting cookies (as follows) - so as to show the latest status of the cookie:
header('Location: '.$_SERVER['PHP_SELF']);

Hence, Please use the following code:

<?php
// extract($_POST);
 $code = $_POST['code'];
 $total= $_POST['total'];
 if( isset($_POST["save"]) ){

    if ($_POST["save"]=="1") {
        echo "Saved";
        $cookie = array(
            'name'   => $code,
            'value'  => $total,
    );
    setcookie("cookie",json_encode($cookie),time()+3600);
}
header('Location: '.$_SERVER['PHP_SELF']);
die;
}

 if(isset($_COOKIE['cookie'])){
        echo "SET";

$getcookie=json_decode($_COOKIE['cookie']);
echo "<br>Name:". $getcookie ->name;
echo "<br>Value:". $getcookie ->value;
echo "<br>";

    }else{
        echo "NOT SET";
    }

?>

<form method='post' action=#>
     <input type="checkbox" name="save" value="1">Save (must tick to save as cookie)
     <input name='code' placeholder='Enter Name'/>
     <input name='total' placeholder='Enter a value'/>

     <input type=submit>
</form>

Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • Thank you so much, the only problem was that i didn't include time()+ in the setting the cookie – james Mar 23 '22 at 08:13
  • one last thing is how would i retreive and show all the values of the cookie ? – james Mar 23 '22 at 08:21
  • meaning how would i let the same cookie have more than one value, and then display all the values ? – james Mar 23 '22 at 08:29
  • Simply use `$getcookie=json_decode($_COOKIE['cookie']);` , then `$getcookie ->name` and `$getcookie ->value` will be what you want (I have amended my answer above to show this) – Ken Lee Mar 23 '22 at 08:39