how-to submit form with multiple groups of radio buttons? - php

goal: post several groups of radio buttons to array (as a message, to database, etc.)
Hi there,
I was wondering how i can post my selections of several groups of radio buttons.
Here is the code:
<body>
<form action="" method="post">
<select name="module 1">
<input type="radio" name="radioA" value="Radio 1">Radio 1
<input type="radio" name="radioA" value="Radio 2">Radio 2
<input type="radio" name="radioA" value="Radio 3">Radio 3
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 2">
<input type="radio" name="radioB" value="Radio 21">Radio 21
<input type="radio" name="radioB" value="Radio 22">Radio 22
<input type="radio" name="radioB" value="Radio 23">Radio 23
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 3">
<input type="radio" name="radioC" value="Radio 31">Radio 31
<input type="radio" name="radioC" value="Radio 32">Radio 32
<input type="radio" name="radioC" value="Radio 33">Radio 33
<input type="submit" name="submitC" value="Get Selected Values" />
</select>
<br>
</form>
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['radioA']))
{
echo "You have selected :".$_POST['radioA']; // Displaying Selected Value
}
}
?>
</body>
...But I can't make it display the list of my selections, but only one instructions, instead of 3x (one for each).
Long story short... How can I treat this as a 1 global set of 3 groups, having only 1 "submit" button to process all 3 groups at once?
Txs a lot, and have a nice weekend.

You just need to remove the other two submit buttons and only display one. As long as it is inside the <form> it will submit all the radio button "groups" to the $_POST.
Also, remove the select tags as you are using them incorrectly. Only an option or optgroup should be nested inside a select element.
You will need to reference each one individually to see the selected response:
echo $_POST['radioA'];
echo $_POST['radioB'];
echo $_POST['radioC'];

Related

radio buttons value if statement

I have several groups of radio buttons that I want to use in an IF statement (or if you have a better solution)
Users will come to the site, select the buttons, then select submit. After submitting, I want the user to see instantly if they should "refer patient" or "don't refer patient".
I am not sure of a couple of things:
How do I make the "submit" button cause the input to be calculated (meaning, the user gets the instant response)
Since there are several combinations of inputs that can create a "refer" or "don't refer" response, can I add multiple conditions to the IF statement? Also, how can I include radio buttons in the statement - do I just use the "value" of the button. I only learned the very basic method of using numbers..
Below is my code so far. I tried to start the IF statement with values. Not sure if doing it right.
Any help is greatly appreciated!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Indications for Mohs</title>
<?php
$Patient_status='unchecked';
$Primary_status='unchecked';
$Type_status='unchecked';
$BCCT_status='unchecked';
$SCCT_status='unchecked';
$Size_status='unchecked';
$Area_status='unchecked';
if (isset($_POST['Submit'])) {
$selected_radio=$_POST['REFER'];
if (selected_radio == "Healthy" && "Primary" && "BCC" && "Aggressive" && "<0.6" && "H" or "Immunocompromised" && "Primary" && "BCC" && "Aggressive" && "<0.6" && "H")
?>
</head>
<body>
<form name="Indications" action="" method="POST">
<p><h2><strong><u>Indications for Mohs Surgery</u></strong></h2>
</p>
<strong>Patient </strong>
<div alighn="center"><br>
<input type="radio" name="Patient" value="Healthy">Healthy<br>
<input type="radio" name="Patient" value="Immunocompromised">Immunocompromised<br>
<input type="radio" name="Patient" value="Genetic">Genetic Syndrome<br>
<hr>
<strong>Primary vs Recurrent</strong>
<div alighn="center"><br>
<input type="radio" name="Primary" value="Primary">Primary<br>
<input type="radio" name="Primary" value="Recurrent">Recurrent<br>
<hr>
<strong>Type</strong>
<div alighn="center"><br>
<input type="radio" name="Type" value="BCC">BCC<br>
<input type="radio" name="Type" value="SCC">SCC<br>
<input type="radio" name="Type" value="LM">LM or MIS<br>
<hr>
<strong>BCC subtype</strong>
<div alighn="center"><br>
<input type="radio" name="BCCT" value="Aggressive">Aggressive<br>
<input type="radio" name="BCCT" value="Nodular">Nodular<br>
<input type="radio" name="BCCT" value="Superficial">Superficial<br>
<hr>
<strong>SCC subtype</strong>
<div alighn="center"><br>
<input type="radio" name="SCCT" value="Aggressive">Aggressive<br>
<input type="radio" name="SCCT" value="Nonaggressive">Nonaggressive<br>
<input type="radio" name="SCCT" value="Verrucous">Verrucous<br>
<input type="radio" name="SCCT" value="KA">KA - type SCC<br>
<input type="radio" name="SCCT" value="Bowen">In situ SCC/Bowen<br>
<input type="radio" name="SCCT" value="AK">AK<br>
<hr>
<strong>Size (cm)</strong>
<div alighn="center"><br>
<input type="radio" name="Size" value="0.5"><0.6<br>
<input type="radio" name="Size" value="0.6-1">0.6-1<br>
<input type="radio" name="Size" value="1.1-2">1.1-2<br>
<input type="radio" name="Size" value="2">>2<br>
<hr>
<strong>Area</strong>
<div alighn="center"><br>
<input type="radio" name="Area" value="H">H<br>
<input type="radio" name="Area" value="M">M<br>
<input type="radio" name="Area" value="L">L<br>
<hr>
<p>
<input type="submit" name="submit" id="submit" value="Submit">
</p>
<p><strong><u>Definitions</u>:</strong><br>
Nonaggressive SCC: <2mm depth without other defining features, Clark level ≤III<br>
Area H: 'Mask Areas' of face (central face, eyelids, eyebrows, nose, lips [cutaneous/mucosal/vermillion], chin, ear, and periauricular skin/sulci, temple), genitalia (including perineal and perianal), hands, feet, nail units, ankles, nipples/areola<br>
Area M: Cheeks, forehead, scalp, neck, jawline, pretibial surface<br>
Area L: Trunk and extremities (excluding pretibial surface, hands, feet, nail units and ankles)</p>
</div>
</form>
</body>
</html>
If you want to have the submit display a response instantly you should use JavaScript as this does not require a form submission/call to a server. You can use the onsubmit event.
Regarding checking for if a radio button is checked, use the .checked property of an element:
document.getElementById('elem').checked //true or false
Almost everything is wrong with that code.
Let's start...
You don't have a REFER element in your form, so $_POST['REFER'] is never set. To access radio button values, you need to access with their relevant name as the index key to the $_POST[] array. E.g. $_POST['Patient'], $_POST['Primary'] ...etc. Those will give you the value of the radio button selected within that group.
Secondly, your conditional statements are wrong in the if statement. To compare conditional statements, you have to specifically compare the variable with different values every time. You'd have to say
if ($selected_radio == "Healthy" && $selected_radio == "Patient") {
// code goes here
}
And also, to check which radio button was selected, you need to access $_POST['<Radio_group_name>'] and this will give you the value of the radio button selected for that group. e.g.
$_POST['Patient']
would give Healthy if user selected that one for the group.

Auto Incremented Name Attribute on Radio Buttons

I have a form that has a set of radio buttons, and I am sending the value of the checked button to another page with php. The form has the option to add another set of the same radio buttons, so if I use the following method of giving name attributes:
<div>
<input type="radio" name="food[]" value="apple" /> Apple
<input type="radio" name="food[]" value="orange" /> Orange
</div>
And add a clone of the above,
$_POST['food'];
This would give me the value of each input, and that would total 4. I'm trying to use the bracket incrementing method but still keep the functionality of returning the value of the radio button that was checked.
So my question is, is there a way to use the square brackets to do auto incrementing on the name attribute, but when sending the values to php make sure each div grouping of radio buttons only returns one of the values for whichever one is checked? Please let me know if this question is too vague, this is my first question here.
May i don't understand your question. You can not Auto Incremented the name cause you use radio buttons. By Choosing apple on the first page and orange on the second you will loose your first choose (apple). You can do it with checkboxes instead of radio buttons:
Page 1:
<form method="post" action="page2.php">
<div>
<input type="checkbox" name="numberofapples" value="1" /> Apple
<input type="checkbox" name="numberoforanges" value="1" /> Orange
</div>
<input type="submit" value="go">
</form>
Page 2:
<form method="post" action="result.php">
<div>
<?if($_POST['numberofapples']){?><input type="hidden" name="numberofapples" value="1" /> <?}?>
<input type="checkbox" name="numberofapples" value="<?=($_POST['numberofapples'])?2:1?>" /> Apple
<?if($_POST['numberoforanges']){?><input type="hidden" name="numberoforanges" value="1" /> <?}?>
<input type="checkbox" name="numberoforanges" value="<?=($_POST['numberoforanges'])?2:1?>" /> Orange
</div>
<input type="submit" value="go">
</form>
Result page:
Apples: <?=($_POST['numberofapples'])?$_POST['numberofapples']:0?>
Oranges: <?=($_POST['numberoforanges'])?$_POST['numberoforanges']:0?>
But why don't you do the counting after posting your second page? (add the post values from page 1 as hidden fields to the form of page 2)
If you want to use several div groups with the same input names, I would create them like this:
<div>
<input type="radio" name="food[0][]" value="apple" /> Apple
<input type="radio" name="food[0][]" value="orange" /> Orange
</div>
<div>
<input type="radio" name="food[1][]" value="apple" /> Apple
<input type="radio" name="food[1][]" value="orange" /> Orange
</div>
<div>
<input type="radio" name="food[2][]" value="apple" /> Apple
<input type="radio" name="food[2][]" value="orange" /> Orange
</div>
So, in your PHP, you will have each group in separated keys. For example
print_r( $_POST['food'] );
Should return something like this:
array(
[0] => array(
[0] => 'apple'
),
[1] => array(
[0] => 'orange'
),
[2] => array(
[0] => 'apple',
[1] => 'orange'
)
)
)

How to allow only 2 selection checkbox in a form with?

I want to put a form on my website for giving away 3 prizes. Each member to be able to select only 2 item on the list that they wish to win.
I have created the form and with JavaScript it works fine, but it's client side and can't be trusted.
What is the best way to implement such thing and be sure that each user can ONLY select 2 item? best way to do it with PHP?
Appreciate any help.
jQuery is boss, have a look here.
http://gravitywiz.com/2012/06/11/limiting-how-many-checkboxes-can-be-checked/
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Price 1<input value="price1" type="checkbox" name="price[]">
Price 2<input value="price2" type="checkbox" value="price[]">
Price 3<input value="price3" type="checkbox" value="price[]">
<input type="submit" name="send" value="Send" />
</form>
Then check the size of the array $_POST['price']:
<?
if(isset($_POST['send'])) {
if(sizeof($_POST['price']) == 2) {
//here you go
} else {
echo "error, pick exactly two prices!";
}
}
?>
Why won't you use radiobuttons?
<form method="post">
Price 1 <input type="radio" value="price 1" />
Price 2 <input type="radio" value="price 2" />
Price 3 <input type="radio" value="price 3" />
</form>
And if you want to allow multiple prizes just switch to checkboxes
<form method="post">
Price 1 <input type="checkbox" name="price[]" value="price 1" />
Price 2 <input type="checkbox" name="price[]" value="price 2" />
Price 3 <input type="checkbox" name="price[]" value="price 3" />
</form>

Add hidden input types based on radio button selected

Is it possible to conditionally add hidden input fields into a form?
eg I have a php form that is adding values to a table and if the appleID = 1 or 2 then I want 1 added to the fruits column of my table and if appleID =3 I want 1 added to the sweets column of my table. I thought I might be able to do something like the below but it is adding all hidden values no matter what I select. Or should I approach this a different way?
<input type="radio" value="1" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="2" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="3" name="appleID" />
<input type="hidden" value="0" name="fruits" />
<input type="hidden" value="1" name="sweets" />
Thanks I haven't done much with php so I will need to explore that option further. Thanks for the feedback.
I was also looking at something like the below. But PHP sounds likes the better option.
change field value when select radio buttons
You can either use all values in the radio buttons (1,1,0 - 2,1,0 - 3,0,1) and split them after receiving them in your PHP script or add/delete the hidden fields via JavaScript.
Split Example:
HTML:
<input type="radio" value="1,1,0" name="appleID" />
<input type="radio" value="2,1,0" name="appleID" />
<input type="radio" value="3,0,1" name="appleID" />
PHP:
if (!empty($_POST['appleID']))
{
list($appleID, $fruits, $sweets) = explode(",", $_POST['appleID']);
}
It is better to put that logic into the PHP script instead of using Javascript - because you have to do the validation anyway.

Only allowing selection of one radio button in a form with PHP

A very basic question...
How can I only allow the selection of one option in a list of radio buttons?
<form action="process_repair.php" method="POST">
<label for "repair_complete">Repair complete</label>
<input type="radio" name="Yes" value="true">
<input type="radio" name="No" value="false">
</form>
When this code runs, it's possible to select both radio buttons, but I'd like them to interact so you can only select one or the other.
Any help much appreciated! :)
Give them the same name.
<form action="process_repair.php" method="POST">
Repair complete
<input type="radio" name="complete" value="true" id="complete_yes" />
<label for="complete_yes">Yes</label>
<input type="radio" name="complete" value="false" id="complete_no" />
<label for="complete_no">No</label>
</form>
Labels must have a for attribute, directing to the corresponding input's id.
Every input should have same "name"

Categories