我查找了许多不同的存储会话变量的方法,似乎无法找到我的会话变量未从我的下拉菜单中存储的原因。这是我的campaign_select.php代码,然后在按下提交按钮后转到store_select.php。
<?php
session_start ();
$host="localhost"; // Host name
$username="sxx2"; // Mysql username
$password="xxxxx!"; // Mysql password
$db_name="sxxxxx"; // Database name
$tbl_name="campaign"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$query = "SELECT campaignname FROM campaign";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$dd .= "<option value='{$row['campaignID']}'>{$row['campaignname']}</option>";
}
if ($_POST['Submit'])
{
if(isset($_POST['campaignname']))
{
$_SESSION['campaign']=$_POST['campaignName'];
}
}
$_SESSION['test']="bye";
?>
<form action="store_select.php" method="post">
<select name="campaignName" id="campaignname"><? echo $dd; ?></select>
<input name="Submit" type="submit" value="Submit">
</form>
这是store_select.php:
<?php
session_start();
$host="localhost"; // Host name
$username="sxxxx2"; // Mysql username
$password="Wxxxx1!"; // Mysql password
$db_name="xxxx2"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$campaign=$_SESSION['campaign'];
echo $_SESSION['campaign'];
echo $_SESSION['test'];
$query = "SELECT storename FROM stores WHERE campaignname='$campaign'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$dd .= "<option value='{$row['storeID']}'>{$row['storename']}</option>";
}
?>
<form action="" method="post">
<select name="stores"><? echo $dd; ?></select>
</form>
答案 0 :(得分:0)
您正在以错误的顺序执行脚本。这就是你在做的事情:
if ($_POST['Submit']) {
if(isset($_POST['campaignname']))
{
$_SESSION['campaign']=$_POST['campaignName'];
}
}
此时 $_POST
为空。只有当用户点击“提交”并转到您的第二个脚本时,$_POST
才会填充您的值。您可以跳过$_POST['Submit']
的检查,这不是必需的。
答案 1 :(得分:0)
替换
<form action="store_select.php" method="post">
带
<form action="" method="post">
和
if(isset($_POST['campaignname']))
带
if(isset($_POST['campaignName']))