javascript - cant set cookie for passing variable to php

i am trying to get the selected answer from the select box in my php skript, for that i found this post were it gets explainded that i can set the variable as a cookie and get it then in php: How to get JavaScript variable value in PHP

i tried this but it is just echo "selected.value":

<select id="carprice" class="d-none" onchange="selected(this)">
    <option value="">Please select</option>
      <option value="10">10</option>
      <option value="20">20</option>
      <option value="30">30</option>
    </select>




 <script type="text/javascript">
       function selected(selected){
          if(selected.value == '10'){
            alert( selected.value ); 
            document.cookie= output=(selected.value);
          }          
        }
      </script>
  <?php
      $output = $_COOKIE['output'];

      print $output;
  ?>
  

thanks

Answer

Solution:

It is not true which you set cookie format. You need to set cookie correctly , with path, with expires date, with name=key format. For example:

<script>
function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}

setCookie('myKey','myVal123',7);
</script>
<?php

print_r($_COOKIE);

Source