0

<input type="radio" name="Wed" value="" checked>None <br />
<input type="radio" name="Wed" value="Class 1">Computers <br />
<input type="radio" name="Wed" value="Class 2" >Physics (1st 5 weeks) <br />
<input type="radio" name="Wed" value="Class 3">Astronomy (2nd 5 weeks) <br />
<input type="radio" name="Wed" value="Class 4">Mathematics <br />
<input type="radio" name="Wed" value="Class 5">Literature <br />

This extract of code is from a form where a student chooses a 10-week course, run on Wednesdays. The radio button works fine and only selects one subject. You will see from the code that Physics and Astronomy are both 5-week courses and run consecutively, and a student may wish to do either course, or both.

So, here is my problem: the radio button at present selects only one of the courses, but I need the code modified so it can select either one of Physics or Astronomy, or both of them.

3
Contributors
2
Replies
22
Views
2 Years
Discussion Span
Last Post by diafol
0

You have to give different sets of radio buttons different name tags. For example:

<input type="radio" name="Wed" value="" checked>None <br />
<input type="radio" name="Wed" value="Class 1">Computers <br />
<input type="radio" name="Wed" value="Class 2" >Physics (1st 5 weeks) <br />
<input type="radio" name="Thu" value="Class 3">Astronomy (2nd 5 weeks) <br />
<input type="radio" name="Thu" value="Class 4">Mathematics <br />
<input type="radio" name="Thu" value="Class 5">Literature <br />

The above would construct two sets of radio buttons, working independently of each other. In short, the answer is to give either the Physics or Astronomy radio button a different name tag.

1

Not sure if you can do that with your current setup. You could have conditional checkboxes...

<?php
$checked = '';
$add = array();
if($_POST)
{
    $checked = $_POST['Wed']; //get value - '',1,4,5 or AdditionalWed
    if($checked && intval($checked == 0))//test for string value
    {
        if(isset($_POST[$checked]))
        {
            $add = $_POST[$checked];    
        }
    }

    echo "POST DATA: <pre>"; 
    print_r($_POST);
    echo "</pre><hr />";

    $classes  = (!empty($add)) ? implode(",",$add) : (($checked) ? $checked : 'None');
    echo "Class(es) for Wednesday: $classes<br /><hr /><br />";

}
?>
<html>
<head>
<style>
    label.block{
        display:block;
    }

    label.indent{
        display:inline-block;
        margin-left: 30px;
    }
</style>
</head>
<body>
<form method="post">
    <label class="block"><input type="radio" name="Wed" value="" <?php if($checked=='') echo "checked";?>> None</label>
    <label class="block"><input type="radio" name="Wed" value="1" <?php if($checked=='1') echo "checked";?>> Computers</label>
    <label class="block"><input id="AdditionalWed" type="radio" name="Wed" value="AdditionalWed" <?php if($checked=='AdditionalWed') echo "checked";?> > Physics and/or Astronomy</label>
    <label class="indent"><input type="checkbox" name="AdditionalWed[]" value="2" disabled <?php if(in_array(2,$add)) echo "checked";?>> Physics (weeks 1-5)</label>
    <label class="indent"><input type="checkbox" name="AdditionalWed[]" value="3" disabled <?php if(in_array(3,$add)) echo "checked";?>> Astronomy (weeks 6-10)</label>
    <label class="block"><input type="radio" name="Wed" value="4" <?php if($checked=='4') echo "checked";?>> Mathematics</label>
    <label class="block"><input type="radio" name="Wed" value="5" <?php if($checked=='5') echo "checked";?>> Literature</label>
    <button>Submit</button>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$('[name = Wed]').change(function()
{
    changeRadio();
}); 

function changeRadio()
{
    var checked = $('#AdditionalWed').is(':checked');
    $('input:checkbox').prop('disabled',!checked);
}

changeRadio();

</script>

</body>
</html>

I don't know if you're using PHP or not, but it's just a proof of concept off the top of my head. - so not tested vigorously. Most of the php here is cosmetic (to display last post data in the form after submission) or to display what was actually sent to the server.

The key here is that non-numeric (but not empty values as in the NONE radio) values from the form are a hook to look for checked checkboxes having that name.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.