【发布时间】:2016-04-07 19:14:13
【问题描述】:
我有一个 index.php 文件,其中包含以下代码,我在其中获取存储在我的数据库中的城市名称打印所有城市并为每个城市打印一个按钮。示例:在我的数据库中包含城市(伦敦、纽约和旧金山)我会得到类似的内容:
伦敦 --> 按钮
纽约 --> 按钮
旧金山 --> 按钮
<?PHP
while($myrow = pg_fetch_assoc($result)) {
$nameofcity = $myrow[cityname];
echo $nameofcity;
$_SESSION['variabletopass'] = $nameofcity;
echo'<form name"formname" method="post" action="results.php">';
echo'<input type="submit" name="submit" value="See City">';
echo'</form>';
}
?>
然后我的 results.php 将在单击按钮时打开。
<html>
<body>
<?php
$var_nameofcity = $_SESSION['variabletopass'];
//Here I look in my database for info base on $var_nameofcity
// something like select description from cities where city = $var_nameofcity
//And I print the info
echo $var_nameofcity;
echo 'Here it goes the Picture of the city';
echo 'description';
?>
</body>
</html>
我的问题是:自从
$_SESSION['variabletopass'] = $nameofcity;
echo'<form name"formname" method="post" action="results.php">';
echo'<input type="submit" name="submit" value="See City">';
echo'</form>';
在一段时间内,$_SESSION['variabletopass'] 将只获得最后一个值,所以无论我按下哪个按钮,它都会打开我的最后一个城市(在示例中:旧金山)。
我该怎么办,所以按下按钮时传递的变量 ($_SESSION['variabletopass'] = $nameofcity;) 是与对应城市关联的变量?
非常感谢
【问题讨论】:
标签: php session variables button