简体   繁体   English

如何记住当前点击的网址? javascript?PHP?

[英]How to remember the currently clicked url? javascript?PHP?

I have a html select list menu,when user select an option he/she is redirected to the page of choice. 我有一个html选择列表菜单,当用户选择一个选项时,他/她将重定向到选择的页面。 for example he/she will be redirected to http://example.com/towns/Faizabad below is the selection menu,this acts as a jump menu. 例如,他/她将被重定向到http://example.com/towns/Faizabad,下面是选择菜单,此菜单用作跳转菜单。

  <select name="cities">
  <option value="towns/Antaliya" id="city1">Antaliya</option>
  <option value="towns/Faizabad" id="city2">Faizabad</option>
  </select>

I need when user(logged/anonymous) selects a city... to add attributed selected, and remember the option for several days until user changes again to another option. 我需要在用户(登录/匿名)选择城市时添加要选择的属性,并记住该选项几天,直到用户再次更改为另一个选项。 I am not good with Javascript/jquery.. i think it can be achived with cookies. 我对Javascript / jquery不太满意。我认为可以通过Cookie来实现。 Any help will be appreciated. 任何帮助将不胜感激。

Correct, you can use a cookie. 正确,您可以使用Cookie。

setcookie("myTown", $_POST['cities'], time()+3600*48);  // expires in 48 hours

In your form you can use 您可以使用表格

if(isset($_COOKIE["myTown"])) {
   echo "<option value=\"" . $_COOKIE["myTown"] . "\">Your Town</option>";
}

Or you can redirect them to the town directly, by using 或者,您可以使用将它们直接重定向到城镇

if(isset($_COOKIE["myTown"])) {
   header("Location: http://yoursite.com/towns/" . $_COOKIE["myTown"])
}

You can find more information about cookies here: http://php.net/manual/en/function.setcookie.php 您可以在此处找到有关cookie的更多信息: http : //php.net/manual/en/function.setcookie.php

You can use this plug-in http://www.ashishblog.com/blog/jquery-cookie-example/ then set your cookies after cities selection 您可以使用此插件http://www.ashishblog.com/blog/jquery-cookie-example/,然后在选择城市后设置Cookie

$('select[name=\'cities\']').change(function(){
$.cookie('city', $(this).val());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM