0

I have been trying to get this to work for a while now... All of the codes are in the same php page/file

<?php
$selectedoption = $_POST["selectedoption"];
$cookie_name = 'selected_option';

setcookie($cookie_name, $selectedoption, time() + (86400 * 30), "/");

if(!isset($_COOKIE[$cookie_name])) {
    echo "Option not Selected";
} else {
    echo "Option is " . $_COOKIE[$cookie_name];
}
?>

<form method="post" action="">
    <input type="radio" name="selectedoption" value="option1">&nbsp;Option 1<br>
    <input type="radio" name="selectedoption" value="option2">&nbsp;Option 2<br>
    <input type="radio" name="selectedoption" value="option3">&nbsp;Option 3<br>
    <input type="radio" name="selectedoption" value="option4">&nbsp;Option 4<br>
    <input type="submit" name="submit" value="Submit">  
</form>

Can cookies be set in the same file as the post? I am not sure if this is supposed to work like this, or if am doing something wrong. Thanks

1 Answers1

0

I have solved this, yay took me an hour xD

First for anyone who is trying to use setcookie() in php and unable to do it, read this PHP - setcookie(); not working

With that said I was still unable to use php, instead I used Javascript to setcookie.

<script>document.cookie = "cookie_name="+'<?php echo $selectedoption; ?>'</script>
echo "Auction Item is a  " . $_COOKIE["cookie_name"]; 

Glad I figured this out myself soon, cuz it didn't seem like anyone cared for this post lmao, so anyone who runs into this solution, you are lucky :D