简体   繁体   English

如何从 PHP 验证动态单选按钮

[英]How to validate dynamic radio button from PHP

How to validate radio button if at least one in the question is not answered.如果问题中至少有一个没有得到回答,如何验证单选按钮。 If you wondered I use while loop in html to display all the questions.如果您想知道我在 html 中使用 while 循环来显示所有问题。 I'm trying the server side validation because I have no idea on how to validate in Javascript.我正在尝试服务器端验证,因为我不知道如何在 Javascript 中进行验证。

Sample Question Output Do you have a fever or temperature over 38 °C?示例问题 Output 您是否发烧或体温超过 38 °C? * Yes No --other questions here * 是 否 -- 其他问题在这里

 <div class="form-group"> <label for="" name="qid[<?php echo $row['id']?>]"><?=$row['questions']?> *</label> <div class="form-check"> <input class="form-check-input" type="radio" name="ans[<?php echo $row['id']?>" value="Yes"> <label class="form-check-label">Yes</label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="ans[<?php echo $row['id']?>" value="No"> <label class="form-check-label" value="No">No</label> </div> </div>

 foreach($_POST as $key => $val) { if(substr($key, 0, 3) == 'ans') { $key = substr($key,4); // $sql2 = "INSERT INTO health_declaration (patient_id,question_id,answer) VALUES ('$patient_id','$key','$val') "; // $query_run1 = mysqli_query($conn,$sql2); } }

* Moved from the comment to answer section. *从评论移到答案部分。

Add the required attribute to all input tags.将 required 属性添加到所有输入标签。

eg:例如:

<input type="radio" ... required>

https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/required https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/required

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM