简体   繁体   English

PHP-从“选择选项”框中保留会话变量到每个页面

[英]PHP - Hold Session variable from Select Option box to every page

I seem to be having a problem and after hours of searching and messing with different ideas I can't seem to come up with this seems to be simple PHP Session problem with my Select Options menu. 我似乎遇到了问题,经过数小时的搜索并弄乱了不同的想法,我似乎无法通过选择选项菜单解决这个简单的PHP Session问题。

All I want to do is simple have the selected Hotel Name from the dropdown hold till it is changed, and this goes for every page. 我想要做的就是简单地从下拉菜单中选择酒店名称,直到更改为止,这适用于每个页面。 So I thought, of yea lets just use Sessions. 所以我想,是的,我们只使用Sessions。 Doesn't seem to work very well as it keeps refreshing the data. 似乎不断刷新数据,效果似乎不太好。 Here is my code, and maybe one of you guys can come up with a solution. 这是我的代码,也许你们中的一个可以提出解决方案。 I would really appreciate it! 我真的很感激!

We are using a json file to populate the menu that we get by using a CURL to call the data and then decode the json. 我们正在使用json文件填充通过使用CURL调用数据然后解码json所获得的菜单。

Here is the dropdown code: 这是下拉代码:

<select id="hotelSelected" name="hotelSelected">
<?php
sort($hotelName);
foreach($hotelName as $value):
echo '<option value="'.$value.'"'.ucfirst($value).'</option>';
endforeach; ?>
</select>

And then I tried to create a session by doing this: 然后我尝试通过执行以下操作来创建会话:

$_SESSION['hotelName'] = $hotelName;

And now I need this session to carry on every page. 现在,我需要该会话来进行每个页面。 And possibly echo on certain pages by possibly using this: 并可能通过使用此方法在某些页面上回显:

echo $_SESSION['hotelName'];

Any help is appreciated or redirect me to a forum that can fix my problem. 感谢您的任何帮助,或将我重定向到可以解决我的问题的论坛。 Thank you guys! 感谢大伙们!

you should use session_start(); 您应该使用session_start(); at the top of every PHP file which uses sessions. 在使用会话的每个PHP文件的顶部。 Also if you have a form contains SELECT tag , after submitting the form you should use $_POST['hotelSelected'] for grabbing tha value of option selected by user. 另外,如果您的表单包含SELECT标记,则在提交表单后,应使用$_POST['hotelSelected']来获取用户选择的选项的值。

EDIT 1: 编辑1:

    <select id="hotelSelected" name="hotelSelected">
    <?php
    sort($hotelName);
    foreach($hotelName as $key=>$value):
    echo '<option value=($_SESSION['hotelname'][$key]):"'.$_SESSION['hotelname'][$key].'"'.ucfirst($_SESSION['hotelname'][$key]).'?"'.$value.'"'.ucfirst($value).'</option>';
    endforeach; ?>
    </select>
<?php
    $_SESSION['hotelname']=$hotelname;

html html

<select id="hotelSelected" name="hotelSelected" onchange="run(this)">
<?php
    sort($hotelName);
    foreach($hotelName as $value):
        echo '<option value="'.$value.'"'.ucfirst($value).'</option>';
    endforeach; 
?>
</select>

javascript javascript

function run(sel) {
    var i = sel.selectedIndex;
    if (i != -1) {
        $.post('updatesession.php', {id:i}, function(){
        alert('Session Updated!');
    });
    }
}

updatesession.php updatesession.php

if(isset($_POST['id'])){
    session_start();
    $_SESSION['hotelName']=id;
}

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

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