Cannot store multiple radio button values in php and mysql - html

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')");
}

Related

Woocommerce bookings

I'm trying to create custom steps on checkout page with Woo bookings plugin.
So I have 3 steps.
create booking product with parameters like duration, start date, end date.
Extra services
Payment
My problem is that I need to submit form details, in other words add this booking product to cart, but without submitting order.
I'm using this structure with hardcoded values, but whem I'm submitting this it acts like place order button, not like add to cart button. How do I just add to cart?
<div class="form-group">
<input type="radio" id="Terminal1withcancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal1withcancelation">terminal 1</label>
<input type="radio" id="Terminal1nocancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal1nocancelation">terminal 1</label>
<input type="radio" id="Terminal2withcancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal2withcancelation">terminal 2</label>
<input type="radio" id="Terminal2nocancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal2nocancelation">terminal 2</label>
<input type="hidden" name="wc_bookings_field_duration" value="4">
<input type="hidden" name="wc_bookings_field_start_date_month" value="07">
<input type="hidden" name="wc_bookings_field_start_date_day" value="23">
<input type="hidden" name="wc_bookings_field_start_date_year" value="22">
<input type="hidden" name="wc_bookings_field_start_date_to_month" value="07">
<input type="hidden" name="wc_bookings_field_start_date_to_day" value="26">
<input type="hidden" name="wc_bookings_field_start_date_to_year" value="2022">
</div>
<button type="submit" name="btn1" value="btn1" class="buy custom_add_btn" id="nextBtn" onclick="nextPrev(1)">NEXT</button>

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

selected option is not hitting to database using magento

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

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

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