0

I have problems with setting a cookie in variable - I have to make it by PHP not JS because I need this variable on another subpages to change content of pages and display products and product variations depend on city choise, but this is not a content of this problem.

The problem is that i can't figure out how to make this variable by sending a form. I tried with below code but it doesn't work.

First solution:

        if(isset($_POST['submit'])){
            $city = htmlentities($_POST['city']);
            setcookie('city', $city, time()+600); 
        }

Second:

        if (isset($_POST['Madrid'])) {
            $city = htmlentities($_POST['city']);
            setcookie('city', $city, time()+600); 
        } else if (isset($_POST['London'])) {
            $city = htmlentities($_POST['city']);
            setcookie('city', $city, time()+600); 
        } else if (isset($_POST['Paris'])) {
            $city = htmlentities($_POST['city']);
            setcookie('city', $city, time()+600); 
        }
   

Form:

    <form method="post" action="">
        <input name='city' type="submit" value='Madrid'>
        <input name='city' type="submit" value='London'>
        <input name='city' type="submit" value='Paris'>
    </form>
mat
  • 130
  • 2
  • 2
  • 12
  • That code should work. Why do you think it doesn't? – Barmar Oct 19 '21 at 15:19
  • Make sure you haven't produced any output before calling `setcookie()`. Check he PHP error log for "Headers already sent" warnings. See https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Barmar Oct 19 '21 at 15:20
  • I put both solutions before also tried below prints after form submit: print_r($city); - Undefined variable: city in ... print_r($_COOKIE); - there is not any $city or name of city – mat Oct 19 '21 at 15:33
  • There is no `$_POST['Madrid']`. I think you mean `if ($_POST['city'] == 'Madrid')` – Barmar Oct 19 '21 at 15:34
  • 1
    All your `if` blocks are doing the same thing. Why do you need them? Just do `if (isset($_POST['city'])) { setcookie('city', $_POST['city'], time()+600); }` – Barmar Oct 19 '21 at 15:36
  • Oh I didn't know that, thank you! – mat Oct 19 '21 at 15:45
  • 1
    The keys in `$_POST` correspond to the `name` in the HTML input, not the `value`. – Barmar Oct 19 '21 at 15:46
  • I noticed that after a form submission and page reload I need one more page reload to get this cookie. Do you have any ideas how to make it after first reload? – mat Oct 20 '21 at 12:32
  • You should't need a second reload to get the cookies into the client. I suspect you may be talking about [this](https://stackoverflow.com/questions/6970754/why-are-my-cookies-not-setting/18936052#18936052) – Barmar Oct 20 '21 at 15:21
  • When I send the form, the page is reloaded. During loading, a cookie is assigned to the system but i cant do logic if cookie is assigned. I need one more page reload to do logic if cookie is set :( – mat Oct 21 '21 at 09:33
  • if (!isset($_COOKIE['city'])) { $_COOKIE['city'] = $_POST['city']; setcookie('city', $_POST['city'], time()+3600, COOKIEPATH, COOKIE_DOMAIN); } this code works – mat Oct 21 '21 at 10:23
  • I can't think of any reason why the cookie wouldn't be set when the page is reloaded. – Barmar Oct 21 '21 at 15:19

0 Answers0