dongshaoxiong0012 2014-03-31 17:48
浏览 99

在重新加载或更改页面PHP之后,使select标签中的选项保持不变

So im having problems on figuring out how to make option tag so when is pressed it will make the action on all the pages This is what i have done:

index1.php
 include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>

index2.php
 include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>

select.php

<div id="regContainer">
    <form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
        <select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off"> 
            <option  value="miami">miami</option>
            <option  value="new york">ny</option>
        </select>  
        <input type="submit" value="Enter Region" />
    </form>  
</div> 


process.php
<?php if(!isset($_SESSION)){ session_start();}


    if(isset($_POST['selectedRegion'])){

    $region = $_POST['selectedRegion'];

    if($region == "miami"){
    echo "miami selected";

    $title = "miami";

    }

    if($region == "new york"){
    echo "new york selected";

    $title = "new york";

     }


    }
?>

when I press the first option, it changes the title normally on the index1.php, but when i go to the index2.php... it does not change the title of that page, I have to press the first option button again to make the change. Is there anyway to just press the first option from the selection and then change the title of all the pages without having to press the button again?

Any suggestion will help. Thanks

  • 写回答

2条回答 默认 最新

  • doumangzhen7204 2014-03-31 17:55
    关注

    Using the code you currently have you could do the following

    <form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
            <select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off"> 
                <option <?php if($_REQUEST['selectedRegion'] == 'miami') echo "selected"; ?>  value="miami">miami</option>
                <option <?php if($_REQUEST['selectedRegion'] == 'newyork') echo "selected"; ?>  value="newyork">ny</option>
            </select>  
            <input type="submit" value="Enter Region" />
        </form>
    

    This will ensure the option is selected on page refresh. If you want something that also acts on page change then you will need to use a cookie

    if(isset($_REQUEST['selectedRegion'])){
        setcookie("region", $_REQUEST['selectedRegion'], time()+3600);
    }
    
    <form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
                <select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off"> 
                    <option <?php if($_COOKIE["region"] == 'miami') echo "selected"; ?>  value="miami">miami</option>
                    <option <?php if($_COOKIE["region"] == 'newyork') echo "selected"; ?>  value="newyork">ny</option>
                </select>  
                <input type="submit" value="Enter Region" />
            </form>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行