John Davidson

php - Cookie not being saved when checkbox is clicked

0 comments
Message:


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


My Answer:
"Checkbox clicked!";_ $cookie_value = array('code' => $code, 'total' => $total);_ setcookie('my_cookie', json_encode($cookie_value), time() + 3600, '/');_ } else {_ echo "Checkbox not clicked!";_ }_ ?>

Make sure that your form has a method of "post" and that the checkbox input has a name attribute of "save" so that the isset() function can check if it has been clicked. Also, make sure that the code you provided is within a form tag and that the form is being submitted when the checkbox is clicked.

Rate this post

5 of 5 based on 5444 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us