我有一个下拉菜单,并在帖子提交后在顶页中定义如下:
if ($_SERVER['REQUEST_METHOD' ] == 'POST' ) {
$usertype = clean($_POST['usertype']);
$useroption = array('admin', 'author', 'public');
以我的形式在这里是代码:
<tr>
<td><label for="usertype">User Type</label></td>
<td><select name="usertype" id="usertype">
<option disabled selected>Please choose the user right</option>
<?php foreach($useroption as $val): ?>
<option
value="<?php echo $val; ?>"
<?php echo (isset($error, $usertype) && $usertype == $val) ? 'selected="selected"' : ''; ?>
>
<?php echo ucfirst($val); ?>
</option>
<?php endforeach; ?>
</select></td>
</tr></form>
但是我收到了消息注意:在我的脚本中第20行的E:\ EasyPHP \ data \ localweb \ projects \ Gamma \ add-user.php中的未定义索引:usertype:
$usertype = clean($_POST['usertype']);
请帮我解决导致此问题的代码或遗漏的内容
我已经尝试删除我的清理功能,但结果仍然相同。但是万一你想知道我的干净代码是什么呢:
function clean($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES);
$data = str_replace(array("<strong>", "</strong>"), array("<strong>", "</strong>"), $data);
return $data;
}
答案 0 :(得分:1)
$_POST[]
中没有 usertype 索引,请尝试检查
$usertype = (isset($_POST['usertype']) ? clean($_POST['usertype']) :'');
答案 1 :(得分:0)
将选项更改为:
<option selected >Please choose the user right</option>