因此,我在确定如何制作选项标签方面存在问题,因此按下它时会在所有页面上执行操作 这就是我所做的:
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";
}
}
?>
当我按下第一个选项时,它会在index1.php上正常更改标题,但是当我转到index2.php ...它不会更改该页面的标题时,我必须按第一个选项按钮再次进行更改。 无论如何只需按下选择中的第一个选项,然后更改所有页面的标题而无需再次按下按钮?
任何建议都会有所帮助。感谢
答案 0 :(得分:1)
使用您目前拥有的代码,您可以执行以下操作
<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>
这将确保在页面刷新时选择该选项。如果您想要一些也会改变页面的内容,那么您需要使用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>
答案 1 :(得分:0)
您需要更改
if(!isset($_SESSION)){ session_start();}
只是
session_start();
您目前所说的是如果您有会话集,请不要加载会话库然后使用该会话数据。