选项值重置

时间:2014-04-17 17:16:41

标签: php forms validation

尝试找出在表单验证失败后删除所选选项值的原因(特别是不正确的验证码)。

如果用户输入了错误的验证码,则会删除在提交前选择的选项值,并默认为第一个值。

<select name="How-soon-are-you-looking-to-invest-in-a-franchise" />
<option value="Within-6-months" <?php if ($_POST["Within-6-months"]=="Within-6-months"){ echo "selected"; } ?>>Within the next 6 months</option>
<option value="Six-twelve-months" <?php if ($_POST["Six-twelve-months"]=="Six-twelve-months"){ echo "selected"; } ?>>6-12 months</option>
<option value="Twelve-or-more-months" <?php if ($_POST["Twelve-or-more-months"]=="Twelve-or-more-months"){ echo "selected"; } ?>>More than 12 months</option>
        </select>

1 个答案:

答案 0 :(得分:3)

表单变量名称已传输,因此您的条件应如下所示:

<option value="Within-6-months" <?php if ($_POST["How-soon-are-you-looking-to-invest-in-a-franchise"]=="Within-6-months"){ echo "selected"; } ?>>Within the next 6 months</option>

简而言之,请使用$_POST["How-soon-are-you-looking-to-invest-in-a-franchise"],而不是$_POST["Within-6-months"]