dongzhi9032 2011-07-21 07:04
浏览 189
已采纳

如果没有选中单选按钮,如何提交表单

I using PHP in order to create an HTML form.
The form has 3 radio buttons and a submit button.
In the initial state of the form, none of the radio buttons is checked.
I want to make the form not to be submitted if non of the radio buttons is checked.
Is there any way to do that? I attached part of the PHP code.

    for($i=0; $i<$nChoices; $i++) {
$Text='<input type="radio" name="answer" value="' . $IdChoices[$i] . '"';
$Text.=">  ". $Choices[$i] . "</input>";
$choiceoutput.= $Text."<br /><br />";
}

Thanks!

  • 写回答

3条回答 默认 最新

  • dseax40600 2011-07-21 07:22
    关注

    This javascript can be used for simple client-side validation. It's not considered safe to only validate on the client-side but if your purpose is to see if the user actually selected an value this will do. Otherwise further validation needs to be taking place in your php script.

    <script type="text/javascript">
    function validate()
    {
      var retval = false;
      for (var i=0; i < document.myForm.r.length; i++)
      {
        if (document.myForm.r[i].checked)
        {
          retval = true;
        }
      }  
    
      return retval;
    }
    </script>

    Simple form for putting it to work.

    <form name="myForm" action="page.php" onsubmit="return validate()" method="post">
    <input type="radio" id="r">
    <input type="radio" id="r">
    <input type="submit" value="ok">
    </form>
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器