Multiple checkboxes with one name (no jquery!) - html

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.

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

For loop - bind dynamic key to the #id

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"

Html multidimensional array in

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">

Payment options with radio buttons in the contact form

My form is currently set up to gather all the input data to my autoresponder...however, I made the form with only one option - pay now. Users would like options, so Im thinking of giving them 2 choices, the old "pay now" COD method, and option#2 paypal. I think radio buttons are the best way for doing this. However I cant get them to work separately...when I choose option 2, option 1 remains selected. So I added the radio buttons myself after the ordernow button.
<p>mail: *</p>
<p>
<label>
<input type="text" class="wf-input wf-req wf-valid__email" name="mail" class="mj" ></input>
</label>
</p>
<p>name: *</p>
<p>
<label>
<input type="text" class="wf-input wf-req wf-valid__required" name="name" class="mj" ></input>
</label>
</p>
<p>
<input type="submit" value="ORDER NOW" class="butt">
<div class="selectpaymentradios">
<label class="radio" >select payment</label>
<input class="radio" type="radio" name="cash" value="cash" checked /> <span>Ca$h</span>
<input class="radio" type="radio" name="ppal" value="ppal" /> <span>PaypaL</span>
</div>
<input type="hidden" name="webform_id" value="12x45"/>
</p>
</form>
<script type="text/javascript" src="http://xyz.com/view_webform.js?wid=12x45&mg_param1=1"></script>
Im trying to figure out how can I make this work with my autoresponder, I think this form has to be able to tell me what kind of payment did the customer chose...but the autoresponders form creator doesnt have radio buttons at all so Im stuck, I dont know if its possible...
<input class="radio" type="radio" name="cash" value="cash" checked /> <span>Ca$h</span>
<input class="radio" type="radio" name="ppal" value="ppal" /> <span>PaypaL</span>
the problem you hit, is very simple - you have to use the same name for all radio-buttons, where only one item should be selected. like this:
<input class="radio" type="radio" name="payment" value="cash" checked /> <span>Ca$h</span>
<input class="radio" type="radio" name="payment" value="ppal" /> <span>PaypaL</span>
The name attribute should be the same for both radio buttons:
<input class="radio" type="radio" name="method" value="cash" checked="checked" /> <span>Ca$h</span>
<input class="radio" type="radio" name="method" value="ppal" /> <span>PaypaL</span>
Also, if you are closing input tags, you are probably worried about XHTML validation. So instead of just checked you should type checked="checked".

Create array from submitted values

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