Jump to content

[SOLVED] How to use the script to read what user last choice was?


ghurty

Recommended Posts

This is a out of my depth, and I am not sure if it is even php related, but perhaps someone can help.

 

I have a script that calculates sunset and sunrise based on a premade list of cities.

As of now, I have it that it defaults to New York, and a user can change it to display a different city's info.

But it does not save this information. Is there a way, (perhaps by using a cookie) that I can have it remember the users last location?

 

Thanks

 

The code I have so far to default it to New York:

if (isSet($_REQUEST["activelocation"])) {
$activelocation = $_REQUEST["activelocation"];
} else {
$activelocation = "New York, NY";

Link to comment
Share on other sites

Yep as u said u can use cookies.

 

if(isset($_COOKIE['location'])){
      $location = $_COOKIE['location']; //if the cookie is set, get the value
} else{
      setcookie('location', 'new york', time()+60*60*24*30); //set the cookie to the default value and
                                                                                //make it expire after aprox. 1 month
}

Link to comment
Share on other sites

Thank You,

From my understanding, this tells it to read the cookie, and if there is no cookie, then to put the default location (new york).

But how do I tell it to write a cookie if the user changes to a different city from the dropdown list I have premade.

 

Thanks

 

Dropdown list:

<form name="mainform" action="index.php" method="get">
<input type="hidden" name="option" value="com_luach" />
<select name="activelocation" onchange="submit();">
<?php fillLocationList($activelocation); ?>
</select>

 

 

Yep as u said u can use cookies.

 

if(isset($_COOKIE['location'])){
       $location = $_COOKIE['location']; //if the cookie is set, get the value
} else{
       setcookie('location', 'new york', time()+60*60*24*30); //set the cookie to the default value and
                                                                                 //make it expire after aprox. 1 month
}

Link to comment
Share on other sites

So I set it up like this

 

if($_GET['activelocation'])
  setcookie('location', $_GET['activelocation'], time()+60*60*24*30);
  
if	(isSet($_COOKIE['location'])){
       $activelocation = $_COOKIE['location']; //if the cookie is set, get the value
} else{
       setcookie('location', 'New York, NY', time()+60*60*24*30); //set the cookie to the default value and
                                                                                 //make it expire after aprox. 1 month

However, now what it does is when I choose a new location it reloads the page, but doesnt put the new location their, rather, the last location  I had, and then when I change it again, it does the one I had just had it on!?

 

Thanks

 

 

Before anything else...

 

if($_GET['activelocation'])
  setcookie('location', $_GET['activelocation'], time()+60*60*24*30);

Link to comment
Share on other sites

make smth like:

 

if($_GET['activelocation'])
  $activelocation = $_GET['activelocation'];
  setcookie('location', $activelocation, time()+60*60*24*30);
} elseif(isset($_COOKIE['location'])){
       $activelocation = $_COOKIE['location']; //if the cookie is set, get the value
} else{
       setcookie('location', 'New York, NY', time()+60*60*24*30);
}

 

So u make progressive checking. If the user has changed the location then set the cookie. If not the first then check if the cookie exists to set the location. If not any of both set the cookie to its default value.

Link to comment
Share on other sites

When I try inserting that, it now gives me

"Parse error: parse error, unexpected '}'"

in this line:

} elseif(isset($_COOKIE['location'])){

 

 

Thank you

 

make smth like:

 

if($_GET['activelocation'])
  $activelocation = $_GET['activelocation'];
  setcookie('location', $activelocation, time()+60*60*24*30);
} elseif(isset($_COOKIE['location'])){
       $activelocation = $_COOKIE['location']; //if the cookie is set, get the value
} else{
       setcookie('location', 'New York, NY', time()+60*60*24*30);
}

 

So u make progressive checking. If the user has changed the location then set the cookie. If not the first then check if the cookie exists to set the location. If not any of both set the cookie to its default value.

Link to comment
Share on other sites

Thank you.

That worked. But now I am running into two problems.

1) If I erase the cookie so that it is as if it is the first time a user is accessing the site, it gives an error, and does not display the information, HOWEVER, once i refresh, it will the display properly.

 

2) Once the cookie is set, and a user visits the site, it displays everything properly, but gives the error:

Notice: Undefined index: activelocation in ......

HOWEVER, if I attempt to change the city using the drop down list, then no error appears at all.

 

Thank You for having patience with me.

For the record, this is the current code

if($_GET['activelocation']){
  $activelocation = $_GET['activelocation'];
  setcookie('location', $activelocation, time()+60*60*24*30);
} elseif(isset($_COOKIE['location'])){
       $activelocation = $_COOKIE['location']; //if the cookie is set, get the value
} else{
       setcookie('location', 'New York, NY', time()+60*60*24*30);
}

Link to comment
Share on other sites

Well I fixed the first problem by adding the following inserting after this:

} else{
       setcookie('location', 'New York, NY', time()+60*60*24*30);

 

This"

$activelocation = "New York, NY";

 

 

But I am still having the second problem, I know what causing it, but how do I tell it to on run that line, only if a change was made?

 

Thanks

Link to comment
Share on other sites

I got rid if the first error, but not the second.

 

The error is: Notice: Undefined index: activelocation in /...../...php on line 32

which refers to this line:

if($_GET['activelocation']){

 

The error only happens in some cases, for example.

If I am just accessing the page, it displays the error, (but continues to load the page) however, if I then go and choose from the drop down list a new city, it loads the page perfectly fine with out the error.

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.