【问题标题】:How to make sure at least one radio button is selected in each group, php如何确保在每组中至少选择一个单选按钮,php
【发布时间】:2013-05-20 03:42:56
【问题描述】:

所以我试图为线程的每个条目组合一个投票系统。每个条目都有一组单选按钮(1、2、3),底部有一个提交按钮。我希望人们投票以确保他们为每个条目选择三个单选按钮之一。我以为我的代码可以正常工作,但事实并非如此。如果选择了最后一个条目,而其他所有条目都没有,它仍然说它很好。但是,如果我不选择它正在工作的最后一个条目。

<form action="vote.php" method="POST" name="form1"> 
<? $sql = "SELECT * FROM contest_entries WHERE contest_id='$contest_id' ORDER BY id desc";  
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); 


while ($list = mysql_fetch_assoc($result)) { $username=$list['username']; 
$date=$list['date_entered']; 
$pl_holder=$list['place_holder1']; 
$contest_entry_id=$list['id']; 


echo "1<input name='attending[$contest_entry_id]' type='radio' value='1'>  
2<input name='attending[$contest_entry_id]' type='radio' value='2'> 
3 <input name='attending[$contest_entry_id]' type='radio' value='3'> />";  
}?> 

<input type="submit" name="submit2" id="submit" value="Submit" />

然后点击提交后在我的 vote.php 页面上:

foreach($_POST['contest_entry_id'] as $key => $something) {  
$example = $_POST['attending'][$key]; 


}  if (!isset($example))  { 
    echo "You need to vote for all entries"; 
exit(); 
}else{ 
echo "success!"; 
}  

除了最后一个条目外,如果最后一个条目被选中而其他条目没有被选中,它仍然认为所有条目都已被选中

【问题讨论】:

    标签: php radio-button


    【解决方案1】:
    1. 您应该在单选选项之前添加具有相同名称的隐藏值,或者再次查询 db 的 id 以对所有选项进行适当的迭代。
    2. 检查每个组是否不同于 foreach 循环中的 0/isset()。

    简单的解决方案:

        ...
        echo '<input type="hidden" name="' . attending[$contest_entry_id] . '" value="0">
        1<input type="radio" name="' . attending[$contest_entry_id] . '" value="1">  
        2<input type="radio" name="' . attending[$contest_entry_id] . '" value="2"> 
        3<input type="radio" name="' . attending[$contest_entry_id] . '" value="3">';
        ...
    

    投票.php

        foreach ($_POST['attending'] as $id => $value) {  
            if ($value == 0) {
                echo 'You need to vote for all entries'; 
                exit; 
            }  
        }  
        echo "success!";  
    

    顺便说一句:如果您认为变量不存在,请不要为变量(例如 $example)赋值 - 直接使用 isset($_POST[...]) 进行检查

    【讨论】:

      【解决方案2】:
      foreach($_POST['contest_entry_id'] as $key => $something) {  
              $example = $_POST['attending'][$key];
      

      这应该如何工作?您的广播组名称为 attending[contest-id] - 所以未定义 $_POST['contenst_entry_id'] - 是吗?

      您的 if/else 条件应位于 foreach-loop 括号内。

      除此之外,我无法告诉您任何事情 - 请发布错误或打印 global $_POST,然后再迭代以查看 var_dump()print_r() 的内容。

      【讨论】:

      • 不,它被命名为参加[contest_entry_id]。它必须是不同的名称,否则如果我为一个条目选择一个单选按钮,然后尝试为不同的组选择另一个单选按钮,它只选择一个,而不是允许所有条目都有自己的选择。
      • 好吧,但这是/不在代码片段中......但是你的 if/else 部分应该在 foreach 循环内
      • 好吧,现在看起来像这样: foreach($_POST['contest_entry_id'] as $key => $something) { $example = $_POST['attending'][$key]; if (!isset($example)) { echo "你需要为所有条目投票";出口(); }else{ 回声“成功!”; } } 越来越近!除了现在,如果只选择了前一个条目,它会打印出:“成功!您需要为所有条目投票”但如果我没有选择顶部条目并选择其他条目,它会正确打印:“您需要为所有条目投票。”
      • 它是在评论部分进行抽象...请更新您的代码并告诉我里面的内容:print_r($_POST) 请
      • 为什么不使用“selected=selected”作为一个单选按钮......这将确保没有任何内容是空的
      猜你喜欢
      • 1970-01-01
      • 2020-10-19
      • 2018-05-11
      • 2011-05-01
      • 2014-02-07
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多