I want to allow only 1 radio button every time - html

Im using radio buttons, and I dont want that the user can select multiple radio buttons. But its not working, the user can select both radio buttons everytime.
Ja = Yes, nee = No.
I want to make the user decide between either Yes or no. Nothing else.
<p>Vraag 13: Houdt u van wijnproeferijen?</p>
<input type="radio" id="vraag13ja" value="ja"> Ja
<input type="radio" id="vraag13nee" value="nee"> Nee
<p>Vraag 14: Houdt u van restaurants?</p>
<input type="radio" id="vraag14ja" value="ja"> Ja
<input type="radio" id="vraag14nee" value="nee"> Nee
<p>Vraag 15: Houdt u van nieuwe mensen ontmoeten?</p>
<input type="radio" id="vraag15ja" value="ja"> Ja
<input type="radio" id="vraag15nee" value="nee"> Nee
<p>Vraag 16: Houdt u van cafes?</p>
<input type="radio" id="vraag16ja" value="ja"> Ja
<input type="radio" id="vraag16nee" value="nee"> Nee
<p>Vraag 17: Houdt u van een feestje?</p>
<input type="radio" id="vraag17ja" value="ja"> Ja
<input type="radio" id="vraag17nee" value="nee"> Nee
<p>Vraag 18: Houdt u van nieuwe culturen ontdekken?</p>
<input type="radio" id="vraag18ja" value="ja"> Ja
<input type="radio" id="vraag18nee" value="nee"> Nee
<p>Vraag 19: Houdt u van bergwandelen?</p>
<input type="radio" id="vraag19ja" value="ja"> Ja
<input type="radio" id="vraag19nee" value="nee"> Nee
<p>Vraag 20: Houdt u van sportwedstrijden?</p>
<input type="radio" id="vraag20ja" value="ja"> Ja
<input type="radio" id="vraag20nee" value="nee"> Nee
<p>Vraag 21: Houdt u van oude gebouwen bezoeken?</p>
<input type="radio" id="vraag21ja" value="ja"> Ja
<input type="radio" id="vraag21nee" value="nee"> Nee
<p>Vraag 22: Houdt u ervan om nationale monumenten te bezoeken?</p>
<input type="radio" id="vraag22ja" value="ja"> Ja
<input type="radio" id="vraag22nee" value="nee"> Nee
<p>Vraag 23: Houdt u van watersport?</p>
<input type="radio" id="vraag23ja" value="ja"> Ja
<input type="radio" id="vraag23nee" value="nee"> Nee
<p>Vraag 24: Houdt u van sporten?</p>
<input type="radio" id="vraag24ja" value="ja"> Ja
<input type="radio" id="vraag24nee" value="nee"> Nee
</div>

From the w3schools.com reference of radio buttons:
Note: The radio group must have share the same name (the value of the
name attribute) to be treated as a group. Once the radio group is
created, selecting any radio button in that group automatically
deselects any other selected radio button in the same group. You can
have as many radio groups on a page as you want, as long as each group
has its own name.
Yo might want to use the label tag as well. For example:
<p>Vraag 13: Houdt u van wijnproeferijen?</p>
<input type="radio" name="first" id="vraag13ja" value="ja">
<label for="vraag13ja">Ja</label>
<input type="radio" name="first" id="vraag13nee" value="nee">
<label for="vraag13nee">Nee</label><br>
<p>Vraag 14: Houdt u van restaurants?</p>
<input type="radio" name="second" id="vraag14ja" value="ja">
<label for="vraag14ja">Ja</label>
<input type="radio" name="second" id="vraag14nee" value="nee"> Nee
<label for="vraag14nee">Nee</label>
...and so on

Related

Radio button text click result wrong answer [duplicate]

This question already has an answer here:
2 sets of radio buttons with same IDs
(1 answer)
Closed 10 months ago.
When I click the text of "prefer not to say" of the second and third questions, the first question's answer changes to "prefer not to say". When I click the text of 'other' of the third question, the second question's answer changes to 'other'. In both cases, the third question's 'other' and 'prefer not to say' do not check when I click text. What is wrong with this code?
It runs well when I click the radio button.
<div>
<p><strong>1. What is your age?</strong></p>
<input type="radio" id="Under18" name="h11" value="Under18">
<label for="Under18">Under 18</label><br>
<input type="radio" id="18-25" name="h11" value="18-25">
<label for="18-25">18 - 25</label><br>
<input type="radio" id="26-35" name="h11" value="26-35">
<label for="26-35">26 - 35</label><br>
<input type="radio" id="46-55" name="h11" value="46-55">
<label for="46-55">46 - 55</label><br>
<input type="radio" id="Over55" name="h11" value="Over55">
<label for="Over55">Over 55</label><br>
<input type="radio" id="Prefer not to say" name="h11" value="Prefer not to say">
<label for="Prefer not to say">Prefer not to say</label>
</div>
<div>
<p><strong>2. What is your gender?</strong></p>
<input type="radio" id="Female" name="h12" value="Female">
<label for="Female">Female</label><br>
<input type="radio" id="Male" name="h12" value="Male">
<label for="Male">Male</label><br>
<input type="radio" id="Other" name="h12" value="Other">
<label for="Other">Other</label><br>
<input type="radio" id="Prefer not to say" name="h12" value="Prefer not to say">
<label for="Prefer not to say">Prefer not to say</label>
</div>
<div>
<p><strong>3. What is your ethnicity?</strong></p>
<input type="radio" id="White/Caucasian" name="h13" value="White/Caucasian">
<label for="White/Caucasian">White/Caucasian</label><br>
<input type="radio" id="Hispanic/Latino" name="h13" value="Hispanic/Latino">
<label for="Hispanic/Latino">Hispanic/Latino</label><br>
<input type="radio" id="Black/African American" name="h13" value="Black/African American">
<label for="Black/African American">Black/African American</label><br>
<input type="radio" id="Asian/Pacific Islander" name="h13" value="Asian/Pacific Islander">
<label for="Asian/Pacific Islander">Asian/Pacific Islander</label><br>
<input type="radio" id="Native American/American Indian" name="h13" value="Native American/American Indian">
<label for="Native American/American Indian">Native American/American Indian</label><br>
<input type="radio" id="Other" name="h13" value="Other">
<label for="Other">Other</label><br>
<input type="radio" id="Prefer not to say" name="h13" value="Prefer not to say">
<label for="Prefer not to say">Prefer not to say</label>
</div>
This is because your radio inputs have the same ID.
Every element needs to have unique ID, so just change some of them and you should be fine. Don't forget to update your for attributes aswell.
IDs are global in the DOM.
Your label is targeting the control with the id "Prefer not to say", so the first control with the ID that matches the for attribute in the label is activated.
Add a 1, 2, 3, etc... to the ids and for attri

Problem with special character in controller in Laravel

I am trying to save multiple checkbox values, and its value attributes contain special characters, such as "ç," "ã," and so on. But when I get into my controller and return the $request object, I see that the special characters are not being formatted in utf-8. How can I solve this, please?
HTML
<div class="form-check">
<input class="form-check-input" name="servicos[]" type="checkbox"
value="Substituição de piso nos banheiros" id="servicos1">
<label class="form-check-label" for="flexRadioDefault1">
Substituição de piso nos banheiros
</label>
</div>
<div class="form-check">
<input class="form-check-input" name="servicos[]" type="checkbox"
value="Instalação de piso na sala e dormitórios" id="servicos2">
<label class="form-check-label" for="flexRadioDefault1">
Instalação de piso na sala e dormitórios
</label>
</div>
<div class="form-check">
<input class="form-check-input" name="servicos[]" type="checkbox"
value="Substituição de piso na cozinha" id="servicos3">
<label class="form-check-label" for="flexRadioDefault1">
Substituição de piso na cozinha
</label>
</div>
Controller
return $request->servicos;

Html, thymleaf radiobutton th:field="" fails if data is null

I´d like to autofill receipt data. If my receipt is not null it should pick the right gender to a radio button with thymeleaf. There are no errors if the receipt is not null, but if receipt is null the errors are thrown
<div class="form-group">
<div id="genderReceipt">
<label for="genderMaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderMaleReceipt" value="MALE" th:field="*{receipt.gender}"/>
Herr
</label>
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderFemaleReceipt" value="FEMALE" th:field="*{receipt.gender}"/>
Frau
</label>
</div>
<label for="genderReceipt" class="error" style="display: none;"> Bitte gib eine Anrede an.</label>
</div>
The result should be like: if receipt == null both are unchecked... if receipt != null the right gender is checked
error log says:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'receipt' available as request attribute
My Solution for this problem is to duplicate the code and insert th:if statements like here in the example
<div th:if="${receipt} == null">
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderMaleReceipt" value="MALE" />
Herr
</label>
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderFemaleReceipt" value="FEMALE" />
Frau
</label>
</div>
<div th:if="${receipt} != null">
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderMaleReceipt" value="MALE" th:field="*{receipt.gender}"/>
Herr
</label>
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderFemaleReceipt" value="FEMALE" th:field="*{receipt.gender}"/>
Frau
</label>
</div>
I know thats not best practice ;( but i didn´t know a other way to do it.

Multiple checkboxes with one name (no jquery!)

I have a following list of checkboxes:
<input type="checkbox" name="day_of_week" value="1">Monday
<input type="checkbox" name="day_of_week" value="2">Tuesday
<input type="checkbox" name="day_of_week" value="3">Wednessday
<input type="checkbox" name="day_of_week" value="4">Thursday
<input type="checkbox" name="day_of_week" value="5">Friday
<input type="checkbox" name="day_of_week" value="6">Saturday
<input type="checkbox" name="day_of_week" value="7">Sunday
After user submits the full form, I receive it in another file:
$week_days = mysqli_real_escape_string($this->db->conn_id, $_POST['day_of_week'])
But then $week_days only contains the value of the last checked checkbox, not all of them. How can I receive all the values?
The name should be an array.
<input type="checkbox" name="day_of_week[]" value="1">Monday
<input type="checkbox" name="day_of_week[]" value="2">Tuesday
<input type="checkbox" name="day_of_week[]" value="3">Wednessday
<input type="checkbox" name="day_of_week[]" value="4">Thursday
<input type="checkbox" name="day_of_week[]" value="5">Friday
<input type="checkbox" name="day_of_week[]" value="6">Saturday
<input type="checkbox" name="day_of_week[]" value="7">Sunday
Hope this helps.
For your second error, mysqli_real_escape_string accepts second parameter as string and you are passing array. Please check this
string mysqli_real_escape_string ( mysqli $link , string $escapestr )
Please use for loop to solve that error.

Cannot store multiple radio button values in php and mysql

I want to store radio button values for each question to my database. I ran it and it stores only for the first question. I know that it may be wrong using if and elseif. The user answers in all questions how can I store them all in mysql table? Does it need to use for loop?
saveunit.php
<?php
include'connect.php';
if(isset($_POST['u1'])){
$u1 = $_POST['u1'];
mysql_query("INSERT INTO unit_form (u1) VALUES ('$u1')");
}
elseif(isset($_POST['u2'])){
$u2 = $_POST['u2'];
mysql_query("INSERT INTO unit_form (u2) VALUES ('$u2')");
}
?>
unitform.php
<form action="saveunit.php" method="POST">
U1 I have found the unit intellectually interesting and stimulating : <br />
1 <input type="radio" name="u1" value="1"> 2 <input type="radio" name="u1" value="2"> 3 <input type="radio" name="number" value="u1"> 4 <input type="radio" name="u1" value="4"> 5 <input type="radio" name="u1" value="5"> <br /> <br />
U2 I have gained knowledge that I consider valuable : <br />
1 <input type="radio" name="u2" value="1"> 2 <input type="radio" name="u2" value="2"> 3 <input type="radio" name="u2" value="3"> 4 <input type="radio" name="u2" value="u2"> 5 <input type="radio" name="u2" value="5"> <br /> <br />
U3 I have acquired skills and abilities that I consider valuable : <br />
1 <input type="radio" name="u3" value="1"> 2 <input type="radio" name="u3" value="2"> 3 <input type="radio" name="u3" value="3"> 4 <input type="radio" name="u3" value="4"> 5 <input type="radio" name="u3" value="5"> <br /> <br />
<input type = "Submit" name = "Submit1" value = "Submit">
<input type="reset" name="reset" value="Clear">
I dont think that you need an if and an else if. It stores the u1 because it always execute the first if(), that's why it always store the first data . What you need to do is to have all the units in one if().
Try this...
if(isset($_POST['u1'])){
$u1 = $_POST['u1'];
$u2 = $_POST['u2'];
$u3 = $_POST['u3'];
$u4 = $_POST['u4'];
$u5 = $_POST['u5'];
mysql_query("INSERT INTO unit_form (u1,u2,u3,u4,u5) VALUES ('$u1','$u2','$u3','$u4','$u5')");
}