这是html..。
<form name="stream" method="post" action="scripts/validate.php">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="attend" value="yes" autocomplete="off">
<label class="form-check-label" for="attend">Yes, I will attend the event in Edo State.</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="trad" value="yes" autocomplete="off">
<label class="form-check-label" for="trad">Yes, I'm interested in getting the Asoebi.</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="house" value="yes" autocomplete="off">
<label class="form-check-label" for="house">Yes, I am interested in getting Accommodation.</label>
</div>
<button class="btn bg-bur text-white h4 btn-block mt-20" type="submit" name="stream">Submit</button>
</form>
下面是处理表单数据的php,但它仍然显示相同的结果“我想要完整的注册”,即使选中了1个复选框或没有选中任何复选框。
<?php
if( isset( $_POST['stream'] ) ) {
if ($_POST['attend'] == 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] == 'yes' ) {
echo "I want the full registration";
}
elseif ($_POST['attend'] == 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] !== 'yes' ) {
echo "I want the Asoebi and I will attend";
}
elseif ($_POST['attend'] == 'yes' && $_POST['trad'] !== 'yes' && $_POST['house'] !== 'yes' ) {
echo "I am only attending";
}
elseif ($_POST['attend'] !== 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] !== 'yes' ) {
echo "I only want Asoebi";
}
elseif ($_POST['attend'] !== 'yes' && $_POST['trad'] !== 'yes' && $_POST['house'] !== 'yes' ) {
echo "You haven't selected anything";
}
}
?>
发布于 2018-06-09 06:11:33
当您选中该框时,$_POSTthe name-not-id将返回该值。有些恼人的是(对我来说),未选中的复选框根本不会返回任何内容。因此,以an为例:
<input class="form-check-input" type="checkbox" name="attend" value="yes">
如果选中此框,则在$_POST“the”中返回"yes“;如果未选中此框,则在$_POST中不返回任何值。因此,您可以使用多个测试来查看它是否被选中。例如:
if (!empty($_POST["attend"])) ...
或
if (isset($_POST["attend"]) && $_POST["attend"] === "yes") ...
发布于 2018-06-09 08:19:26
终于让它起作用了。
<?php error_reporting(0); if( isset( $_POST['stream'] ) ) {
if ($_POST['attend'] == 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] == 'yes' ) {
echo "I want the full registration";
}
elseif ($_POST['attend'] == 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] !== 'yes' ) {
echo "I want the Asoebi and I will attend";
}
elseif ($_POST['attend'] == 'yes' && $_POST['trad'] !== 'yes' && $_POST['house'] !== 'yes' ) {
echo "I am only attending";
}
elseif ($_POST['attend'] !== 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] !== 'yes' ) {
echo "I only want Asoebi";
}
elseif ($_POST['attend'] !== 'yes' && $_POST['trad'] !== 'yes' && $_POST['house'] !== 'yes' ) {
echo "You haven't selected anything";
} }?>
https://stackoverflow.com/questions/50768840
复制相似问题