我有一排复选框,我的目标是有一个脚本,检查三个中是否有至少一个。
目前,我可以通过在复选框中添加名称(例如name="checkbox"
)并使用if(empty($_POST['checkbox']))
来检查是否选中了个人复选框。
这是我当前的代码和我尝试检查是否至少选中了一个复选框的脚本:
<form method="post">
<ul>
<li><input type="checkbox">Example</li>
<li><input type="checkbox">Example</li>
<li><input type="checkbox">Example</li>
</ul>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
$container = $_POST['list'];
if(empty($container)) {
// checkbox checked
}else{
// checkbox not checked
}
}
?>
答案 0 :(得分:2)
此时评论的时间太长,并已在评论中概述了您需要做的事情。
N.B。:我从现在删除了#34;&#34;问题,因为我打算参考它。
请注意,如果您希望为每个复选框使用相同的名称属性,则需要使用[]
将所有复选框视为数组。
即:name="the_name[]"
基于以下内容:
<?php
if(isset($_POST['test'])){
$a_counts = array();
foreach($_POST['test'] as $key => $val){
if(!isset($a_counts[$val])){
$a_counts[$val] = 1;
}else{
$a_counts[$val]++;
}
echo $key." => ".$val."<br>";
}
print_r($a_counts);
}
?>
<form action="" method="POST">
<input type="checkbox" name="test[]" value="red">
<input type="checkbox" name="test[]" value="green">
<input type="checkbox" name="test[]" value="yellow">
<input type="checkbox" name="test[]" value="blue">
<input type="submit" value="ok">
</form>
或类似的东西,这将是更好的解决方案:
<form method="post">
<label class="heading">Select from the following:</label>
<input type="checkbox" name="check_list[]" value="Value 1"><label>Value 1</label>
<input type="checkbox" name="check_list[]" value="Value 2"><label>Value 2</label>
<input type="checkbox" name="check_list[]" value="Value 3"><label>Value 3</label>
<input type="checkbox" name="check_list[]" value="Value 4"><label>Value 4</label>
<input type="checkbox" name="check_list[]" value="Value 5"><label>Value 5</label>
<input type="submit" name="submit" Value="Submit"/>
</form>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
}
}
else {
echo "<b>Please Select at least One Option.</b>";
}
}
?>
<?php
if (!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
}
}
else {
echo "<b>Please Select at least One Option.</b>";
}
答案 1 :(得分:0)
首先,您必须确保从复选框
发布一系列值<form method="post" action="file.php">
<ul>
<li><input name="checkbox[]" type="checkbox">Example</li>
<li><input name="checkbox[]" type="checkbox">Example</li>
<li><input name="checkbox[]" type="checkbox">Example</li>
</ul>
<input type="submit" name="submit">
</form>
接下来,你发布到你的php页面,在这种情况下file.php
<?php
if(isset($_POST['checkbox'])){
//Atleast one is checked
}
?>
或者你可以循环复选框
<?php
$checkboxArray = $_POST['checkbox'];
foreach($checkboxArray as $checkbox){
if(isset($checkbox)){
$isOneSelected = true;
}
}
?>