在我的论坛中,当我添加一个主题时,我有一个包含所有可能类别的下拉列表。当我发布并且其中一个文本字段为空时,会出现错误,我想保持选中下拉列表中的选定项目。
我以前做过,但没有用 Smarty。有人可以看到我做错了什么吗?
$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>