【问题标题】:Redisplay Form with drop down PHP使用下拉 PHP 重新显示表单
【发布时间】:2016-07-14 09:29:00
【问题描述】:

我知道我的查询有很好的记录,但我仍然不确定是否有人可以在这里权衡:我有一个下拉字段“html”或“plain”。

见代码:

       $mail_format = 0;
    if (isset($_POST['mformat'])) {
        $form_is_submitted = true;
        if ($mail_format == 'plain' || $mail_format == 'html') {
            $clean['mformat'] = $_POST['mformat'];
        } else {
            $errors['mformat'] = '***You have not ticked a format***';
            $errors_detected = true;
        }
    if(isset($errors['mformat'])) {
    $mformat_2 = $errors['mformat'];
} else {
    $mformat_2 = '';
}

    if (isset($clean['mformat'])) {
    $mail_format = $clean['mformat'];
} else {
    $mail_format = '';
}
            <label for="mform">Mail format</label>
                        <select name="mformat" id="mform">
                            <option value="plain">Plain text</option>
                            <option value="html">HTML</option>
                        </select>

如果用户没有勾选格式,我如何在上面的消息字段旁边注册错误?同样,如果他们确实勾选了格式但其他字段错误,我如何让它重新显示到用户选择的选项的表单?

提前致谢。

【问题讨论】:

  • 你没有在 post 方法中使用&lt;form&gt; 标签吗?
  • 是的,确实我正在使用带有 post 方法的表单标签返回 $ server 全局数组,但我认为我会忽略所有这些并获取需要配置的代码......跨度>

标签: php html forms display


【解决方案1】:

该脚本有一些问题,您没有分配 $mail_format 并且您的变量名称应该更清晰,但要回答您的一般问题,您可以将错误消息变量写入页面

<div class="error"><?php echo $mail_format_error_msg;?></div>
<select name="mail_format" id="mail_format">
    <option value="plain">Plain text</option>
    <option value="html">HTML</option>
</select>

要保持用户选择,您需要动态编写selected 属性。像这样:

<option value="plain" <?php if (isset($mail_format) && $mail_format=="plain") echo "selected";?>>Plain text</option>
<option value="html" <?php if (isset($mail_format) && $mail_format=="html") echo "selected";?>>HTML</option>

【讨论】:

  • 谢谢!要分配 $mail_format,当我编码:$mail_format = $_POST['mformat']; 时,它会出错说未定义的索引:mformat。另外,用 php 回声 mformat_2; ?> 它没有提供错误。
  • 我的例子是它的一般做法——不能代替你学习基本的 PHP。
猜你喜欢
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多