如果未选中复选框,我试图显示一条错误消息.我已经设法完成输入字段,但是不确定如何使用复选框完成它.这是我到目前为止的内容:
# check if data has been posted
if (!empty($_POST)) {
# keep track validation errors
$author_error = null;
$categories_error = null;
# keep track of post values
$author = $_POST['author'];
$categories = $_POST['categories'];
# validate input
if (empty($author)) {
$author_error = 'Please select author';
$valid = false;
}
if (empty($categories)) {
$categories = 'Please select categories';
$valid = false;
}
# if data is valid, insert into the database
if ($valid) {
}
}
?>
Categories:
foreach ($dbConnection->query($sql3) as $data3) { ?>
<?php if(isset($_POST['categories']) && in_array($data3['id'], $_POST['categories'])) { echo 'checked'; } ?>>
<?php if (!empty($categories_error)) { echo '' . $categories_error . ''; } ?>
我在类别字段哪里出错了?
解决方法:
像这样检查:
isset($_POST['categories'])
标签:post,php,checkbox
来源: https://codeday.me/bug/20191026/1935614.html