简体   繁体   中英

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

this is a snippet from the header.php document where everything is pretty much completed through.

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. You must also call session_start() on any page where you wish to access session variables.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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