我有一个包含多个复选框的表单。我想允许用户在每个复选框字段中选择至少1,如果用户没有选中复选框中的任何选项,则会显示一条消息“您必须至少选择一个”。我该怎么做?我这里有一些代码:
<div>
<div><span class="font-2">Categories:</span></div>
</div>
<div class = "addinfo">
<?php $categories = array('Breakfast', 'Lunch', 'Dinner', 'Snack', 'Grill', 'Buffet', 'Fast Food');
$values = explode(',' , $row['categories']);
?>
<?php foreach($categories as $category) {
$cat='';
foreach($values as $value){
if($value == $category){
$cat ="checked";
}
?>
<input <?php echo $cat ?> type="checkbox" name="category[]" value="<?php echo $category?>"><?php echo $category?><br>
<?php }
}?>
</div>
形式
<form method = "POST" action = "<?php echo base_url().'/index.php/AdminController/operation'?>">
AdminController
public function addResto(){
$this->load->model('AdminModel');
$this->AdminModel->insert();
$this->getRestos();
}
public function operation(){
if(isset($_POST['btn'])){
if(empty($_POST['id'])){
$this->addResto();
}
else{
$this->updatingResto();
}
}
}
public function updateResto($id){
$this->load->model('AdminModel');
$restaurantinfo['restaurantinfo']=$this->AdminModel->getResto($id);
$this->load->view('admin/UpdateRestoPage',$restaurantinfo);
}
public function updatePage(){
$this->load->view('admin/UpdateRestoPage');
}
public function updatingResto(){
$id = $_POST['id'];
$this->load->model('AdminModel');
$this->AdminModel->update($id);
}
答案 0 :(得分:0)
在您的被叫php中添加验证
<?php
$checked = false;
foreach($_POST["category"] as $key) {
if(!empty($key)) {
$checked = true;
}
}
if(!$checked) {
echo "<script>";
echo "alert('You have to select at least one.');";
echo "history.back();";
echo "</script>";
exit();
}
?>
答案 1 :(得分:0)
在控制器操作功能中使用
if (isset($_POST['mycheckbox'])) {
echo "checked!";
} esle {
//send again with error message
}
答案 2 :(得分:0)
在javascript中使用此验证:
$("input:checkbox[name='a']:checked").length == 0
示例:
HTML code:
<from name='frm1' method='' action='POST'>
<label>Favorites:</label>
<span>sports</span><input type="checkbox" name="fav" value="1"/>
<span>music</span> <input type="checkbox" name="fav" value="2"/>
<span>reading</span> <input type="checkbox" name="fav" value="3"/>
<input type="button" value="validate">
</form>
Javascript code:
function validate() {
//replace with your own code here
if ($('input:checkbox[name="fav"]:checked').length == 0) {
alert("You have to select at least one.");
}
}
$("input[type='button']").click(validate)
这只是一个简单的例子来证明你需要使用:check
选择器(JQuery)
改变并将其嵌入您的项目中!
答案 3 :(得分:0)
尝试在您的HTML代码中添加:
public class A
{
public A()
{
// DoSomething
// If something is not correct return null;
}
}
public class B : A
{
public B() :base()
{
// Check if everything was fine in base class
}
}
此页面将在页面刷新前显示错误。并在你的html中添加这样的东西:
<script type="text/javascript">
function check () {
var inputs = document.querySelectorAll("input[type='checkbox']");
for(var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
return true;
}
}
alert("You have to select at least one.");
return false;
}
</script>
更新:尝试用此替换您的代码。
<input type = "button" onclick = "check();">
答案 4 :(得分:0)
您还应该检查是否有checkbox
被检查
public function operation(){
if(isset($_POST['btn'])){
if(empty($_POST['category'])){
$this->load->view('YOUR_VIEW_FILE',array('error'=>'category'));
}else if(empty($_POST['id'])){
$this->addResto();
}
else{
$this->updatingResto();
}
}
}
您的view
文件添加条件
<?php
if(!empty($error) && $error=="category"){
?>
<script>alert("You have to select at least one.");</script>
<?php
}
?>