我正在使用PHP和数据库进行编程,我想要做的是将会话变量传递到另一个页面,但问题是我看不到其他页面上的值是我的选择器代码
<form method="post" action="monitorIndex.php">
<?php
$projectParse = oci_parse($conn, 'SELECT DISTINCT PROJECT_NAME FROM MASTER_DRAWING '
. 'ORDER BY PROJECT_NAME ASC');
oci_execute($projectParse);
echo '<select name="cd-dropdown" id="cd-dropdown" class="cd-select">';
echo '<OPTION VALUE="">PROJECT SELECT</OPTION>';
while($row = oci_fetch_array($projectParse,OCI_ASSOC)){
$projectName = $row ['PROJECT_NAME'];
echo "<OPTION VALUE='$projectName'>$projectName</OPTION>";
}
echo '</select>';
?>
<input type="submit" name="submit">
</form>
,接受者就是这个,
<?php
$_SESSION['cd-dropdown'] = $_POST['cd-dropdown'];
?>
<h1><?php $_SESSION['cd-dropdown']; ?></h1>
我无法看到h1中的值,请帮帮我
答案 0 :(得分:4)
你错过了echo
;
<h1><?php echo $_SESSION['cd-dropdown']; ?></h1>
顺便说一句,你应该以某种方式session_start();
启动会话,否则你的代码将无效
见下面的评论:
$_SESSION['cd-dropdown'] =
isset($_POST['cd-dropdown']) ? $_POST['cd-dropdown'] :
isset($_SESSION['cd-dropdown']) ? $_SESSION['cd-dropdown'] : '';
或(更具可读性)
if(isset($_POST['cd-dropdown'])) {
$_SESSION['cd-dropdown'] = $_POST['cd-dropdown'];
}
elseif(!isset($_SESSION['cd-dropdown'])) {
$_SESSION['cd-dropdown'] = '';
}
请记住,我不知道背后的逻辑,所以你必须自己“修复”逻辑(即:“旧的”发布值怎么样?必须被接受或删除?)