Smarty/PHP我想让下拉列表中的一项保持选中状态,但Smarty不起作用
在我的论坛中,当我添加一个主题时,我有一个包含所有可能类别的下拉列表。当我发布并且其中一个文本字段为空时,会出现一个错误,我希望在下拉列表中保持选中的项目 我以前做过,但不是和Smarty一起做的。有人能看出我做错了什么吗Smarty/PHP我想让下拉列表中的一项保持选中状态,但Smarty不起作用,php,smarty,Php,Smarty,在我的论坛中,当我添加一个主题时,我有一个包含所有可能类别的下拉列表。当我发布并且其中一个文本字段为空时,会出现一个错误,我希望在下拉列表中保持选中的项目 我以前做过,但不是和Smarty一起做的。有人能看出我做错了什么吗 $query_cat = " SELECT fcID ,fcName FROM forum_categories "; $exec_cat = mysql_query($query_cat);
$query_cat = "
SELECT
fcID
,fcName
FROM
forum_categories
";
$exec_cat = mysql_query($query_cat);
while($categories = mysql_fetch_assoc($exec_cat))
{
if(isset($_POST['category']))
{
$selected = ' selected';
}
else
{
$selected = '';
}
}
$this->view->assign('selected', $selected);
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$row = mysql_fetch_assoc($exec_cat);
$subject = mysql_real_escape_string(htmlentities($_POST['subject']));
$content = mysql_real_escape_string(htmlentities($_POST['content']));
$query_add_topic = "
INSERT INTO
forum_topics
(
ftDate
,fcID
,fuID
,ftSubject
,ftMessage
)
VALUES
(
NOW()
,'".$_POST['category']."'
,'".$_SESSION['userid']."'
,'".$subject."'
,'".$content."'
)
";
$exec_add_topic = mysql_query($query_add_topic);
}
else
{
while ($row = mysql_fetch_assoc($exec_cat))
{
$entries[] = $row;
$this->view->assign('entries', $entries);
}
}
聪明的
<table width=100%>
<tr>
<td>Onderwerp:</td>
</tr>
<tr>
<td width="50"><input type="text" name="subject"></td>
</tr>
<tr>
<td>Categorie type</td>
</tr>
<tr>
<td><select name="category">
{foreach from=$entries item=entry}
<option value="{$entry.fcID}"{$selected}>{$entry.fcName}</option>
{/foreach}
</select>
</td>
</tr>
<tr>
<td class="text"><textarea name="content" cols="50" rows="10"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="send" value="Verstuur"></td>
</tr>
OnDerERP:
分类类型
{foreach from=$entries item=entry}
{$entry.fcName}
{/foreach}
有一个Smarty标记,用于在select、
{html\u options}
中创建
标记。它的文档是。除了不必在模板中构建自己的循环之外,还可以将所选值指定为参数,您可以通过$smarty对象从PHP传入该参数。这将使您能够以最小的努力和最大的智慧创建一个
,轻松地为您的主题传递先前选择的值。而不是此
Instead of this
<select name="category">
{foreach from=$entries item=entry}
<option value="{$entry.fcID}"{$selected}>{$entry.fcName}</option>
{/foreach}
</select>
You can use
<select name="category">
{html_options options = $entries selected = $entries.fcName}
</select>
hope so it will work for you
{foreach from=$entries item=entry}
{$entry.fcName}
{/foreach}
你可以用
{html_options options=$entries selected=$entries.fcName}
希望它对你有用