I currently have a form with lets say 5 radio button entries (see below).
What im looking archive is the following:
- Being able to choose multiple radio buttons - lets say 3 and submit the form.
Currently I got it working fine with PHP, SQL, but im only able to choose one radiobutton and submit that.
I figure it would also come in handy being able to deselect a radio button in case you wrongly click one.
My guess is that this can be done through some javascript? Any suggestions? Online examples perhaps?
<form id="pollform" action="poll.php" method="post">
<input id="option-1" type="radio" value="1" name="poll">
<label for="option-1">Select option 1</label>
<input id="option-2" type="radio" value="2" name="poll">
<label for="option-2">Select option 2</label>
<input id="option-3" type="radio" value="3" name="poll">
<label for="option-3">Select option 3</label>
<input id="option-4" type="radio" value="4" name="poll">
<label for="option-4">Select option 4</label>
<input id="option-5" type="radio" value="5" name="poll">
<label for="option-5">Select option 5</label>
</form>
Radio buttons are designed so that only one option from each group (as designated by their shared name) can be selected at once (just like you can only tune a radio to one station).
The input control which allows any number of options to be selected is the checkbox. If you append [] to their names, then the selected options will arrive on the PHP side as an array.
<input type="checkbox" value="1" name="poll[]" />
<input type="checkbox" value="2" name="poll[]" />
As it has same name poll you will not be able to do that as input type radio is specialized in selecting a single value from multiple inputs.
You can use input type checkbox for that and make them as an array:
<form id="pollform" action="poll.php" method="post">
<input id="option-1" type="checkbox" value="1" name="poll[]">
<label for="option-1">Select option 1</label>
<input id="option-2" type="checkbox" value="2" name="poll[]">
<label for="option-2">Select option 2</label>
<input id="option-3" type="checkbox" value="3" name="poll[]">
<label for="option-3">Select option 3</label>
<input id="option-4" type="checkbox" value="4" name="poll[]">
<label for="option-4">Select option 4</label>
<input id="option-5" type="checkbox" value="5" name="poll[]">
<label for="option-5">Select option 5</label>
</form>
LIMIT (with jQuery) the number:
$("input[type=checkbox][name=poll[]]").click(function() {
var numberSel = $("input[type=checkbox][name=poll[]]:checked").length >= 3;
$("input[type=checkbox][name=poll[]]").not(":checked").attr("disabled",numberSel);
});
Radio buttons are designed for only 1 item to be selected, use checkboxes instead.
Related
This question already has an answer here:
I have Multiple radio button with same name in it of array and have same values as 1
(1 answer)
Closed 4 years ago.
i have multiple radio button in same name as array and same value for all.
<input type="radio" name="radio_name[]" id="radi_name" value="1" checked>
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
in php i used like this
$a[]=$_post['radio_name'];
prinr_r($a);
im getting result like this :
Array ( [0] => 1 )
if i uncheck the button set as zero i want result to be like this
Array ( [0] => 1,[1] => 0,[2] => 0,[3] => 0 )
Please check this images i have form like this
You need to use only [] instead of array keys also use checkbox instead of radio . So use following code it will work for you.
<input type="checkbox" name="radio_name[]" id="radi_name1" value="1" checked>
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name2" value="1" >
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name3" value="1" >
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name4" value="1" >
<label for="radio1">Set as Default</label>
I am pretty sure that radio buttons don't get sent to the server if none of the values is selected. In your html you really have 4 different radio button groups so only the ones that have a selected value, get sent.
If you want 4 groups where a value always gets sent for each group, you should do something like this:
<input type="radio" name="radio_name[1]" value="1" id="radio1" checked>
<input type="radio" name="radio_name[1]" value="0">
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[2]" value="1" id="radio2">
<input type="radio" name="radio_name[2]" value="0" checked>
<label for="radio2">Set as Default</label>
// etc.
This way you will have independent radio button groups and you will get the result you want.
You have to use checkbox instead of radio. Radio only allowed 1 value being posted, and remove the array key like dipmala suggested.
I'm trying to make a question paper. My code is like this:
<form action="check.php">
<span id="ques_id_12">Question 1</span>
<input type="radio" id="option_a_12" name="ques12[]" value="1">
<label for="option_a_12">Answer a</label>
<input type="radio" id="option_b_12" name="ques12[]" value="2">
<label for="option_b_12">Answer b</label>
<input type="radio" id="option_c_12" name="ques12[]" value="3">
<label for="option_c_12">Answer c</label>
<input type="radio" id="option_d_12" name="ques12[]" value="4">
<label for="option_d_12">Answer d</label>
<span id="ques_id_13">Question 2</span>
<input type="radio" id="option_a_13" name="ques13[]" value="1">
<label for="option_a_13">Answer a</label>
<input type="radio" id="option_b_13" name="ques13[]" value="2">
<label for="option_22">Answer b</label>
<input type="radio" id="option_c_13" name="ques13[]" value="3">
<label for="option_c_13">Answer c</label>
<input type="radio" id="option_d_13" name="ques13[]" value="4">
<label for="option_d_13">Answer d</label>
<span id="ques_id_14">Question 3</span>
<input type="radio" id="option_a_14" name="ques14[]" value="1">
<label for="option_a_14">Answer a</label>
<input type="radio" id="option_b_14" name="ques14[]" value="2">
<label for="option_b_14">Answer b</label>
<input type="radio" id="option_c_14" name="ques14[]" value="3">
<label for="option_c_14">Answer c</label>
<input type="radio" id="option_d_14" name="ques14[]" value="4">
<label for="option_d_14">Answer d</label>
<input type="submit">
</form>
The question is dynamic and above comes from a php and mysql code. I want to post the data to a php file where I can calculate the number of questions correct, number of questions wrong and number of questions attempted.
I'm confused how should I check the correct answers. $_POST[ans_a] would return ans_a from all questions. How can I distinguish all the questions and separately put them into array. Or should I change my way of arranging my dynamic code ?
Note what #RajdeepPaul said. Your answers are being exposed and posted along with the answers, so if you want any kind of security don't do this, and check for the answers in PHP..
$answers = ['ques12' => 4, 'ques13' => 3, 'ques14' => 2];
Now loop over all the answers. The keys in $answers are the same as in $_POST.
First of all, never put the correct answer as hidden input field in the HTML form. Anybody can see the source code and get the correct option for all the questions. Keep the correct answers(options) corresponding to particular questions in a table like this:
+-------------+----------------+
| question_id | correct_option |
+-------------+----------------+
| | |
Or, use an array like this:
$correct_options = array('ques_id_12' => 4, 'ques_id_13' => 3, 'ques_id_14' => 2);
So that later you could compare the correct answer with the user inputted option value.
Now comes to your question, you need to change the name attribute value of your checkbox elements like this,
name="ques_id_12" for all the checkbox options for question ID 12, name="ques_id_13" for all the checkbox options for question ID 13 etc.
Also, since you're sending bulk of data to the server with your form, you should use POST method instead of GET
So your form should be like this:
<form action="check.php" method="POST">
<span id="ques_id_12">Question 1</span>
<input type="radio" id="option_a_12" name="ques_id_12" value="1">
<label for="option_a_12">Answer a</label>
<input type="radio" id="option_b_12" name="ques_id_12" value="2">
<label for="option_b_12">Answer b</label>
<input type="radio" id="option_c_12" name="ques_id_12" value="3">
<label for="option_c_12">Answer c</label>
<input type="radio" id="option_d_12" name="ques_id_12" value="4">
<label for="option_d_12">Answer d</label>
<span id="ques_id_13">Question 2</span>
<input type="radio" id="option_a_13" name="ques_id_13" value="1">
<label for="option_a_13">Answer a</label>
<input type="radio" id="option_b_13" name="ques_id_13" value="2">
<label for="option_22">Answer b</label>
<input type="radio" id="option_c_13" name="ques_id_13" value="3">
<label for="option_c_13">Answer c</label>
<input type="radio" id="option_d_13" name="ques_id_13" value="4">
<label for="option_d_13">Answer d</label>
<span id="ques_id_14">Question 3</span>
<input type="radio" id="option_a_14" name="ques_id_14" value="1">
<label for="option_a_14">Answer a</label>
<input type="radio" id="option_b_14" name="ques_id_14" value="2">
<label for="option_b_14">Answer b</label>
<input type="radio" id="option_c_14" name="ques_id_14" value="3">
<label for="option_c_14">Answer c</label>
<input type="radio" id="option_d_14" name="ques_id_14" value="4">
<label for="option_d_14">Answer d</label>
<input type="submit">
</form>
And after form submission, you can use simple foreach loop to compare the user inputted option value with the correct option value for each question like this:
foreach($_POST as $question_id => $user_inputted_option){
// compare the user inputted option value with the correct option value
}
Update(1):
How I would know which questions have been answered and which have not ? And how would I pass the question id's ?
Let's say your question id array is like this:
$ques_ids = array('ques_id_12', 'ques_id_13', 'ques_id_14', 'ques_id_15', 'ques_id_16');
Then after form submission, you need to process your form like this:
$array_keys = array_keys($_POST); // all the user attempted question ids
foreach($ques_ids as $q_id){
if(in_array($q_id, $array_keys)){
// attempted question
$user_inputted_option_value = $_POST[$q_id];
// now compare the user inputted option value with the correct option value
}else{
// unattempted question
}
}
Sidenote: If you want to see the complete array structure, do var_dump($_POST);
For my app I need to fetch the data from the database and mark fields accordingly. I am stuck with how to mark the radio button based on the value fetched. I am sharing the code I wrote to fetch the data and the radio button which I need to check.
$Medical_Record_Id=$_SESSION['Medical_Record_Id'];
$DOL=$_SESSION['DOL'];
$hour=$_SESSION['hour'];
$Question1="xxx";
$data1 = 'SELECT Option_Selected FROM Form1 WHERE Medical_Record = "'.$Medical_Record_Id.'" and DOL="'.$DOL.'" and hour="'.$hour.'" and Question="'.$Question1.'"' ;
$query = mysql_query($data1) or die("Couldn't execute query. ". mysql_error());
The html code which I have for the radio button is
<div>
<span>
<input type="radio" name="Gestational_age" class="field checkbox" value="<28" tabindex="7" />
<label class="choice" for="Gestational_age"><28</label>
</span>
<span>
<input type="radio" name="Gestational_age" class="field checkbox" value="28-31" tabindex="7" />
<label class="choice" for="Gestational_age">28-31</label>
</span>
<span>
<input type="radio" name="Gestational_age" class="field checkbox" value=">=32" tabindex="7" />
<label class="choice" for="Gestational_age">>=32</label>
</span>
</div>
Please guide me in how to check the radio button based on the value I am fetching in $query .
First of all, your HTML is really messed up. If you want to use symbols such as >, < in HTML attribute values or as text to display in the labels, you should convert them to the respective HTML entities, e.g., <, >.
After that, if you need to mark a value as selected, you should use the checked attribute on the specific <input> control:
<input type="radio" checked="checked" .... >
In order to show your input checked put some like below, see the second input
<form>
<input type="radio" name="Gestational_age" class="field checkbox" value="<28" tabindex="7">
<input type="radio" name="Gestational_age" class="field checkbox" value="28-31" tabindex="7" checked="checked">
<input type="radio" name="Gestational_age" class="field checkbox" value=">=32" tabindex="7">
</form>
I am developing a PHP website which will generate several approval requests.
My approval page has only one form and one save button, and I want to use 2 radio buttons for each request.
Here is a link to how I imagine it to look like: link
On the PHP side I have no problem with generating the form and assigning different names to the radio buttons. My problem is what names I should give to the radio buttons in order for me to be able to associate them to a specific member on the PHP side? Or do I need to set values for the radio buttons?
How would I accomplish this?
If you have an even better solution to handle such a case I am open to that as well.
Thanks
Every User will have a unique user_id,
As you are displaying radiobuttongroup for each other user in the page, and want to access then in php,
The base way to name them in a array with indices as their user_id
<input type="radio" value="1" name="acceptance['uid1']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid1]"/>Deny
<input type="radio" value="1" name="acceptance['uid2']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid2]"/>Deny
<input type="radio" value="1" name="acceptance['uid3']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid3]"/>Deny
in php
foreach($_POST['acceptence'] as $uid=>$acpt)
{
..Do whatever
}
I hope this would help..If something else is needed plz comment..
Here you are:
<input type="radio" value="approve" name="acceptance"/>Approve<br />
<input type="radio" value="deny" name="acceptance"/>Deny
With radio buttons, they should named the same and have different values for grouping them.
UPDATED
To me, I will add row id to each radio group buttons. For example
<input type="radio" value="19" name="acceptance1"/>Approve<br />
<input type="radio" value="0" name="acceptance1"/>Deny
...
<input type="radio" value="21" name="acceptance2"/>Approve<br />
<input type="radio" value="0" name="acceptance2"/>Deny
...
<input type="radio" value="23" name="acceptance5"/>Approve<br />
<input type="radio" value="0" name="acceptance5"/>Deny
Assuming we have 5 rows per page and each approve radio button holds id of a records in database.
<?php
$approve = array();
for($i=1;$i<=5;$i++)
$approve[] = $_POST['acceptance'.$i];
?>
Then you will get what row has approve selection (i.e value !=0). Hope this helps
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.