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">
Related
$(document).ready(function(){
$('#update2').click(function(){
var checkboxes = $('input[name="checkbox1"], input[name="checkbox2"]');
checkboxes.on("change", function(e){
checkboxes[0].setCustomValidity(checkboxes.filter(":checked").length ? '' : 'please select at least one option to procees')
}).change
});
});
$(document).ready(function(){
$('#select2').click(function(){
var checkboxes2 = $('input[name="choose1"], input[name="choose2"]');
checkboxes2.on("change", function(e){
checkboxes2[0].setCustomValidity(checkboxes2.filter(":checked").length ? '' : 'please select at least one option to procees')
}).change
});
});
<form>
<input type="radio" id="update" name="update-button" value="1">
<input type="radio" id="update2" name"update-button" value="2">
<input type="checkbox" id="check1" name="checkbox1" value="3">
<input type="checkbox" id="check2" name="checkbox2" value="4">
<input type="submit" name="submit-button">
</form>
<form>
<input type="radio" id="select" name="select-button" value="10">
<input type="radio" id="select2" name"select-button" value="11">
<input type="checkbox" id="box1" name="choose1" value="12">
<input type="checkbox" id="box2" name="choose2" value="13">
<input type="submit" name="submit-select">
</form>
Im trying to set custom validation to the checkboxes. so when i select the radio button with the id "select2", the checkboxes with diffrent name "choose1" and "choose2" one of them is required before submitting the form. it is working perfieclty in the first example with "update2" and "checkbox1","checkbox2" checkboxes. but i have no idea why it is not working in the second example with the "select2" and "choose1", "choose2" checkboxes.
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 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.
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')");
}
How do I create 2 HTML arrays from value pairs on form submit?
from <input type="text" name="val_a1" value="1">
to <input type="text" name="val_b1" value="2">
from <input type="text" name="val_b1" value="3">
to <input type="text" name="val_b1" value="4">
from <input type="text" name="val_c1" value="5">
to <input type="text" name="val_c1" value="6">
to look like
array[1,3,5] and array[2,4,6]
Do I need to five unique field names, like in my example or just keep val_a and val_b?
Use brackets in the field names to have the input value return an array:
from <input type="text" name="from[0]" value="1">
to <input type="text" name="to[0]" value="2">
from <input type="text" name="from[1]" value="3">
to <input type="text" name="to[1]" value="4">
from <input type="text" name="from[2]" value="5">
to <input type="text" name="to[2]" value="6">
Note that the keys (0, 1, 2) are optional and could be anything you want (or none at all), but I used them so it would make more sense once you get the return values. You should receive from and to as an array when submitting the form now.
AFAIK you can have several query values with the same name. So it would then depend on what backend you are using to parse the querystring. Common practice is to name several fields that should be grouped together name[], and most backends will turn that into an array then. So try both methods out on your backend and check how they are treated!
if you just want an array you can do it like this
from <input type="text" name="from[]" value="1">
to <input type="text" name="to[]" value="2">
from <input type="text" name="from[]" value="3">
to <input type="text" name="to[]" value="4">
from <input type="text" name="from[]" value="5">
to <input type="text" name="to[]" value="6">
you don't need to number them they will be in order they are recieved
but you can go into nesting too see this post for more details