1

I have been trawling through various examples of radio buttons validation and grasp the concept but no examples seem to fit my situation. I have queried my database which has 4 options and for each option I have generated an associated radio button as below:

<?php foreach($options as $option){ ?>

<p><input type="radio" name="ID<?php echo $row['id']; ?>" value="<?php echo $option; ?>"><?php echo $option; ?></p>

<?php } ?>

So this basically generates 4 radio buttons per row called from the database. This appears in the source as:

<form method="POST" action="Result.php">
    <p>1.  Question </p>
        <p><input type="radio" name="ID1" value="answer1">answer1</p>
        <p><input type="radio" name="ID1" value="answer2">answer2</p>
        <p><input type="radio" name="ID1" value="answer3">answer3</p>
        <p><input type="radio" name="ID1" value="answer4">answer4</p>
    <p>2.  Question 2</p>   
        <p><input type="radio" name="ID2" value="answer1">answer1</p>
        <p><input type="radio" name="ID2" value="answer2">answer2</p>
        <p><input type="radio" name="ID2" value="answer3">answer3</p>
        <p><input type="radio" name="ID2" value="answer4">answer4</p> 
    <input type="submit" name="submit" value="Submit!">          
</form>

etc etc.

The names of these radio buttons are partially generated by php, how can I use javascript with an associated alert to check 1 of the radio buttons from each question has been checked prior to submitting the form the questions are populated within?

Or maybe even just how can I achieve this with php ISSET?

2
  • In your result.php, isset($_POST['ID1']) will return true if one of these radio buttons from the first question was checked. Then $_POST['ID1'] will hold the value of either answer1, answer2 and so on. Commented Feb 9, 2017 at 15:31
  • Eddi, thank you your reply, I understand this would happen within the results.php but my question was how could I verify the form within the same page prior to submitting the form on to results.php sorry for the confusion Commented Feb 9, 2017 at 15:37

1 Answer 1

2

Using just JavaScript to verify whether a radio button within a radio group is checked:

function radiogroupSelected(name){
    var inputArr = document.getElementsByName(name);
    for(var i = 0; i < inputArr.length; i++){
        if(inputArr[0].checked){
            return true;
        }
    }

    return false;
}

Now you can call this function like radiogroupSelected('ID1') to determine if one of the inputs was selected.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help, im used this concept and expanded it for each ID. Appreciate the answer thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.