Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
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. I'm trying the server side validation because I have no idea on how to validate in Javascript.

Sample Question Output Do you have a fever or temperature over 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);
  }
}

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

* Moved from the comments section to the answers section.

In Javascript we create a selector of all the input[type="radio"], we implement a forEach(), we create an event for each radio option, and we verify it with a selected in a conditional.

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!