<label class="radio-inline">
<input type="radio" name="optradio" value="1" <?php echo($data["optradio"]== 1 ?'checked="checked"':''); ?> />
Male </label>
<label class="radio-inline">
<input type="radio" name="optradio" value="2" <?php echo($data["optradio"]== 2 ? 'checked="checked"':''); ?> />
Female </label>
I want to store the selected value of gender in database. The code i have written as shown above.
Please guide me where i am going wrong.
Related
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
I have a for loop that displays the data into multiple radio-buttons (bootstrap):
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" id="choice.id"> [[choice.content]]
</label>
</div>
As you can see, I wanted to use the choice.id for id="..." in each button, technically it should look something like this:
<input type="radio" name="optradio" id="1"> Choice1
<input type="radio" name="optradio" id="2"> Choice2
<input type="radio" name="optradio" id="3"> Choice3
But it renders it with the actual string choice.id:
<input type="radio" name="optradio" id="choice.id"> Choice1
<input type="radio" name="optradio" id="choice.id"> Choice2
<input type="radio" name="optradio" id="choice.id"> Choice3
Forgive my naiveness. Any help/advices? Thanks a lot!
It renders with choice-id string because you add plain string as the id value, not a variable value
You can use v-bind directive or the shorthand for v-bind -> :id
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" v-bind:id="choice.id"> [[choice.content]]
</label>
</div>
using shorthand <input type="radio" name="optradio" :id="choice.id">
To answer your questions in the comments.
You can ' separate 'the radios by adding them in a ' group ' using the name attribute. Radios with the same name attribute are in one group. Changing their values won't affect other radios in other groups ( with different name attributes ). See example below.
Or you can use vue v-model to separate and get the selected options.
new Vue({
el: "#radio-app",
data: {
question1: '',
question2: ''
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://github.com/vuejs/vue-devtools"></script>
Question1:
<input type="radio" name="question1" id="q1choice1" value="choice1"/>
<input type="radio" name="question1" id="q1choice2" value="choice2"/>
Question2:
<input type="radio" name="question2" id="q2choice1" value="choice1"/>
<input type="radio" name="question2" id="q2choice2" value="choice2"/>
<hr />
<h2> Or with VUE v-model </h2>
<div id="radio-app">
Question1:
<input type="radio" id="q1choice1vue" value="choice1" v-model="question1">
<input type="radio" id="q1choice2vue" value="choice2" v-model="question1">
Question2:
<input type="radio" id="q2choice1vue" value="choice1" v-model="question2">
<input type="radio" id="q2choice2vue" value="choice2" v-model="question2">
<div>Question 1 selected answer: {{ question1 }}</div>
<div>Question 2 selected answer: {{ question2 }}</div>
</div>
Check more here
VUE template syntax
v-bind directive
Use: v-bind:id="choice.id" or the short version :id="choice.id"
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.
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')");
}
I would like to pass multiple value with check box,is any provision to pass multiple value using
check box in html.
<input type="checkbox" name="service_id[]" value="1">
please suggest me how should be it is possible
It can be done this way:
HTML
<form method="post">
<input type="checkbox" name="service_id[0][]" value="1">
<input type="checkbox" name="service_id[0][]" value="2">
<input type="checkbox" name="service_id[1][]" value="3">
<input type="checkbox" name="service_id[1][]" value="4">
<input type="checkbox" name="service_id[1][]" value="5">
<input type="submit">
</form>
PHP
<?php
if(!empty($_POST['service_id']))
var_export($_POST['service_id']);
You can only have one value per input. If you want to have multiple values, then either:
give each set of values a unique identifier and resolve it on the server
encode the data in a format like JSON or CSV and then parse it on the server
If you want to have multiple inputs with different values, then just create multiple elements in the HTML.
PHP will discard all but one of them if they come from inputs which share a name and that name doesn't end with [], but your name does:
<input type="checkbox" name="service_id[]" value="1">
<input type="checkbox" name="service_id[]" value="2">
<input type="checkbox" name="service_id[]" value="3">
<input type="checkbox" name="service_id[]" value="4">
<input type="checkbox" name="service_id[]" value="5">
<input type="checkbox" name="service_id[]" value="6">