user1830833
user1830833

Reputation: 181

Not properly retrieve cookie value from html(js) to php file

Okay, so, I've reached a point where my head is about to explode, so I thought someone might know what my problem is. I have a html form with select list. Now, on form submit, I want to set a cookie with the selected value from the select list (with javascript) and read it in the php file and use its value for another variable. When I select one of the options from the drop down and click submit, nothing changes, it's as if the same value is being passed.... I don't know where I am going wrong.

HTML + JS :

<form action="CalendarFeeder3.php" name="cf" method="post">
    <select name="myvalue" id="SelectTimeZone" name="cfd">
        <option value="Africa/Abidjan">Africa/Abidjan</option>
        <option value="Africa/Accra">Africa/Accra</option>
        <option value="Africa/Addis_Ababa">Africa/Addis_Ababa</option>
        <option value="Africa/Algiers">Africa/Algiers</option>
    </select>
<input type="submit" onClick="createCookie('cookieee',selectedValue,'500')">

The JS:

function createCookie(name,value,days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = name+"="+value+expires+"; path=/"+"; domain=.<?php echo $_SERVER['HTTP_HOST']; ?>";

            var selectedValue = document.getElementById("SelectTimeZone").value;
        }

And the PHP:

$kookie = $_COOKIE[_cookieee];
date_default_timezone_set($kookie);

Upvotes: 0

Views: 66

Answers (2)

Laloutre
Laloutre

Reputation: 172

is that the full html/js code ?

Because i don't see where you give a value to "selectedValue"

Also maybe the fact that the value 500 is passed as a string ? I can't recall how javascript handle that, but i'm quite convinced it's not good. try without the quote.

So, to sum up, try with :

onclick=" var sel = document.getElementById('SelectTimeZone'); createCookie('cookieee',sel.options[sel.selectedIndex].value ,500); "

As for the PHP side, i would go for $kookie = $_COOKIE['cookieee'];

And as suggested by marty, remove the domain part of your cookie.

Upvotes: 0

Marty Mulligan
Marty Mulligan

Reputation: 1174

Have you checked your browser to make sure the cookie is actually set? I would do that next...

Lastly, I'd remove the path from the domain part of your cookie in the javascript. A browser is going to try to match the domain it's browsing against that value, so the /fillerexample part may be tripping it up?

Upvotes: 1

Related Questions