简体   繁体   English

下拉菜单设置会话变量

[英]dropdown set session variable

I'm using a wordpress theme that I built and I'm trying to get a dropdown box to set a session variable so I can use it to load a specific container according to the value of the variable 我正在使用自己构建的wordpress主题,并且试图获得一个下拉框来设置会话变量,以便可以根据变量的值使用它来加载特定的容器

this is a snippet from the header.php document where everything is pretty much completed through. 这是header.php文档中的摘录,所有内容都已经完成了。

Every page will contain this data 每页将包含此数据

The problem isn't the session variable being set but it seems it is cleared once I goto another page. 问题不是会话变量被设置,但是一旦我转到另一个页面,它似乎就被清除了。

Please could someone assist me. 请有人帮我。

Thanks, 谢谢,

<?php
$_SESSION['city_selected'] = $_POST['city_select'];
?>
                        <div class="cities">Select City <form method="post" action="<?php echo $PHP_SELF;?>"><select name="city_select" id="city_select" onchange="submit();"><option>-----</option>
                        <option value="Beijing">Beijing</option>
                        <option value="Shanghai">Shanghai</option>
                        <option value="Guangzhou">Guangzhou</option>
                        <option value="Manila">Manila</option>
                        <option value="HongKong">Hong Kong</option>
                        <option value="Tokyo">Tokyo</option>
                        <option value="Seoul">Seoul</option>
                        <option value="Taipai">Taipai</option>
                        </select></form></div>

** EDIT ** **编辑**

<?php if(isset($_POST['city_select'])) { $_SESSION['city_selected'] = $_POST['city_select']; } ?> 

worked for me :) cheers 为我工作:)干杯

<?php if(isset($_POST['city_select'])) { $_SESSION['city_selected'] = 
$_POST['city_select']; } ?>

This worked for me. 这对我有用。

Make sure you have called session_start() before attempting to set any session variables. 在尝试设置任何会话变量之前,请确保已调用session_start() You must also call session_start() on any page where you wish to access session variables. 您还必须在希望访问会话变量的任何页面上调用session_start()

<?php
session_start();
$_SESSION['city_selected'] = $_POST['city_select'];
?>

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

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