2015-10-13 65 views
1

我想要访问html选择标记中的php会话变量,以便我可以使用它来确定如何填充选项。读取html选择标记中的php会话变量

如果我在select标签之外回显变量,它可以正常工作,但它不会在标签内回显。

我没有经验知道为什么。

以下是代码片段,我删除了不相关的代码,以使其更易于阅读。

<?php 
session_start(); 

$_SESSION['profile_type']='test'; 

?> 

<!doctype html> 
<html lang="en"> 

<head> 
    <title>Profile</title> 
</head> 

<?php 
    echo $_SESSION["profile_type"]; //This WILL echo. 

?> 

<select id="profile" name="profile"> 

<?php 

    echo $_SESSION["profile_type"]; //This will NOT echo. 

?> 

</select>  

回答

1

这可能是回声,但除非你看源代码,否则你不能看到它。在<select>字段中,您需要有<option value='foo'>Foo Text</option>行。选项标签是填充选择框的东西。选择框是在提交时用选项值发送的内容。

+0

我在完整的代码中有选项,我不认为这是问题所在。我需要能够读取此会话变量以确定如何填充选项 – user3101337

+0

您能否以此为基础显示您的代码?可能会有一个比较语法错字,或其他的东西。 –

+0

现在我明白了,谢谢。 – user3101337

2

正如Andrew所言,<select>中的内容必须是<option>。所以,为你列出你要找的会话变量,你需要这样的

<select id="profile" name="profile"> 
    <option value="test">Text here</option> 
    <?php 
    echo "<option value=".$_SESSION['profile_type'].">".$_SESSION['profile_type']."</option>"; 
    ?> 
</select> 

注意,超全局变量,如$_SESSION$_GET$_POST不能在字符串中指定,并且必须串接如上。

+0

啊,我现在明白了,对不起,花了这么长时间便士下降。 – user3101337