Multiple radiobutton groups with the same names? - html

Is it possible to have multiple radiobutton groups in one form with the same name values?
I'm working on a quiz which consists of multiple (questions) radiobutton groups like:
<div class="question">
<p><b>Question 19</b></p>
<div class="choices">
<input type="radio" name="choice-for-question-98" id="choice-98-1" value="389">
<label for="choice-98-1">e duhur </label><br>
<input type="radio" name="choice-for-question-98" id="choice-98-2" value="388">
<label for="choice-98-2">qëllim</label><br>
<input type="radio" name="choice-for-question-98" id="choice-98-3" value="387">
<label for="choice-98-3">drejt</label><br>
<input type="radio" name="choice-for-question-98" id="choice-98-4" value="386">
<label for="choice-98-4">e drejtë</label><br>
</div>
</div>
What I want is to get a list of "answer" attributes in POST but when I set the same names for different questions, user can choose only one radiobutton from all questions.

No.
The name is the mechanism used to define which group a radio button belongs to.
You could use similar, but different, names and then look for them on the server with a loop.
e.g.
name="choice-for-question-98-group-1"
and
my $choice;
my $group = 1;
while ($choice = $q->param("choice-for-question-98-group-" . $choice)) {
do_something_with($choice);
$group++;
}

Related

HTML Checkboxes in Form POST

vI am working with HTML and have a form with a group of checkboxes:
-> What are your favorite colors?
---> Red
---> Green
---> Blue
Say I selected, Red AND Green. In the form post, I want it to look like: "favorite-color": ["red", "green"]. Now, it only shows one of the colors, even if I select multiple. Here is some sample code:
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="red" name="color" value="red">
<label class="custom-control-label" for="red">Red</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="green" name="color" value="green">
<label class="custom-control-label" for="green">Green</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="blue" name="color" value="blue">
<label class="custom-control-label" for="blue">Blue</label>
</div>
My post, when I select red AND green:
{'color': 'green'}
By the way, I am using Django as my backend to parse the form POST.
Is there a way to do this? Thanks!
Via this blog:
For multiselects or inlines (one-to-many fields) that have multiple values, use .getlist():
So:
request.POST.getlist("color")
Give all the input type="checkbox" elements the same value for their name attribute and then the values from the checked checkboxes will be submitted as a single value for the one name. In most server-side environments, that means that if you request that form value, you'll get all the values with the single form field request.

Distinguishing checkboxes with the same ID in different classes

Hope you can help me regarding webscraping via VBA. In a webform I have to check and uncheck checkboxes using VBA. Now I run into the difficulty that some checkboxes have the same ID but reside in different classes.
Example:
Class "filter" has a checkbox with ID "start_date" and class "return_fields" has a checkbox with ID "start_date". When using .getElementByID("start_date") both checkboxes switch simultaneously.
But in this case I want to have the checkbox in class "filter" to be checked and the one in "return_fields" to be unchecked.
So I've attempted something like
.getElementsByClassName("filter")(0).getElementById("start_date").checked = True
.getElementsByClassName("return_fields")(0).getElementById("start_date").checked = False
Unfortunately this returns an error "Object doesn't support this property or method".
Here is the content of the element I get with .getElementByClassName("filter"):
<legend>Date Mode</legend>
<div class="start_date required">
<label for="active_date">Start Date</label>
<input name="date_mode" class="radio" id="start_date" type="radio" checked="checked" value="start_date">
</div>
<div class="report_date required">
<label for="report_date">Report Date</label>
<input name="date_mode" class="radio" id="report_date" type="radio" value="report_date">
</div>
<div class="end_date required">
<label for="end_date">End Date</label>
<input name="date_mode" class="radio" id="end_date" type="radio" value="end_date">
</div>

Proper way to read checkbox data in NodeJS

I have a form on my webpage and its being submitted to a NodeJS backend.
I'm having trouble with the checkboxes. When the are submitted to server, and I read them via req.body.foods, I get something like ['on', 'on', 'on'].
But I want to get the actual values, that is, something like ['dairy', 'fish'] etc.
How can I do that?
<div class="col-sm-6">
<div class="checkbox">
<label><input name="food" type="checkbox" value="dairy">Dairy</label>
</div>
<div class="checkbox">
<label><input name="food" type="checkbox" value="meat">Meat</label>
</div>
<div class="checkbox">
<label><input name="food" type="checkbox" value="fish">Fish</label>
</div>
</div>
You can do the following in the node.js file:
console.log(req.body.food); //This will print the array of values.
If you have only one checkbox selected in the page, (eg: Dairy) it will print "dairy". If more than one checkbox, then you get "{ 'dairy', 'meat' }" in the output. Make sure the checkboxes are within the form.
Another method:
Include a hidden input element in your form:
<input name="SelectedValues" type="hidden" value="">
Also include a call to a javascript file in the onchange event of every checkbox, or in the onclick event of the submit button in the form.
onclick='buildlist("YourCheckBoxName","SelectedValues");'
Use a javascript function to loop through your checkbox and build a comma separated list of selected values:
function buildlist(listName,labelName){
var controls = document.getElementsByName(listName);
var label = document.getElementsByName(labelName);
label.value = '';
for(var i=0;i<controls.length;i++){
label.value += controls[i].value.toString()+',';
}
}
Now inside the node.js file, use the following code to access the list of values:
req.body.SelectedValues
Looks like you are using bootstrap to generate your form checkboxes. Try using the "form-check" and "form-check-input" classes instead.
<div class="form-check">
<input class="form-check-input" type="checkbox" name="food" value="dairy" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">Dairy</label>
</div>

Radio input not changing value?

I have a radio input that is not changing values when selected. It is simply saying Domain is the value all the time.
<div class="col-xs-2">
<label class="radio-inline" for="AccountType_Domain"> <input id="AccountType_Domain" name="AccountType" type="radio" value="Domain">Domain</label>
<label class="radio-inline" for="AccountType_Local"> <input id="AccountType_Local" name="AccountType" type="radio" value="Local">Local</label>
</div>
At first I was using #html.RadioButtonFor but that was causing the 2 inputs to have the same id.
When executing $("input:radio[AccountType]").val() I am only getting Domain no matter what is selected.
You have to use the :checked css selector.
So, to get the value of the selected AccountType item in jQuery, it will look as follows:
$('input[name=AccountType]:checked').val()
You need to use:
$("input[name='AccountType']:checked").val()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="col-xs-2">
<label class="radio-inline" for="AccountType_Domain"> <input id="AccountType_Domain" name="AccountType" type="radio" value="Domain" checked>Domain</label>
<label class="radio-inline" for="AccountType_Local"> <input id="AccountType_Local" name="AccountType" type="radio" value="Local">Local</label>
</div>

How to add values in HTML forms?

How would i add the "value" that are selected from radio boxes in html forms? So when someone selects an option it would add the other "values" onto it and total that it at the bottom of the page. And does anyone know if it could add "names" total "values" onto it as well? thanks
My code looks like this:
<h3><u>Title</u></h3><br>
<form action="">
<input type="radio" name="num" value="0">Text<br>
<input type="radio" name="num" value="2">Text<br>
<input type="radio" name="num" value="80">Text<br>
<input type="radio" name="num" value="110">Text<br>
<input type="radio" name="num" value="85">Text<br>
<input type="radio" name="num" value="120">Text<br>
</form>
You cannot. By definition, a set of radio buttons with the same name attribute contributes at most one value to the data set, the one corresponding to the selected button.
If you want something else, you should handle that server side, or use other types of controls, or redesign the entire approach.
Working example :
(using a Javascript library, jQuery, but could be done in plain JavaScript)
You mainly have to change your inputs to type="checkbox" in the HTML
What code does : when a checkbox's state is modified, all checked checkboxes' value are summed up in the last input field I've added
The checkboxes are targetted by looking for "num" in their name, if you remove that the checkbox won't be taken into account by the script.
$(function() {
$("input[name*='num']").on("change", function() {
var total = 0;
$("input[type='checkbox']:checked").each(function() {
total += Number($(this).val());
});
$("#total").val(total);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>
<u>Title</u>
</h3>
<br>
<form action="">
<input type="checkbox" name="num0" value="0">Add 0<br>
<input type="checkbox" name="num2" value="2">Add 2<br>
<input type="checkbox" name="num80" value="80">Add 80<br>
<input type="checkbox" name="num110" value="110">Add 110<br>
<input type="checkbox" name="num85" value="85">Add 85<br>
<input type="checkbox" name="numwhateveryoulike" value="120">Add 120<br>
Total <input type="text" value="0" id="total">
</form>