I am creating a quiz application...
I have created a question text area, and a number of input fields for options..
Each input field has a radio button, if user clicks it, it will consider the associated input field as correct answer..
I have created some input fields and radio button with each input field
<div id="items">
<input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct" value="yes"/> <br><br>
<input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct" value="yes"/>
</div>
when i Submit this form it stores yes with each Option. I am unable to understand how to store the value of only 1 input field.
Kindly guide me
Thanks.
if you get value of options is input1 answer is input1 or if options value is input2 answer is input2
<input type="radio" name="options" value="input1">Is Correct<br>
<input type="radio" name="options" value="input2">Is Correct
input1 is name of textbox 1
input2 is name of textbox 2
Get value on form post :-
$nam=$_POST['options'];
$answer=$_POST[$nam];
You can use input as below :
<div id="items">
<input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct[]" value="yes"/> <br><br>
<input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct[]" value="yes"/>
</div>
Related
my question is how can I keep the radio button checked after I miss filling one of the form fields?
I have a form for the admin to fill out to add a user, I have 3 radio buttons to choose user type, and of course, the rest of the form: user name, age, etc. when the user misses filling one of the fields and click submit, the filled information stay but the radio button get back to the old selection. how can I keep the new selection when correcting the form?
this is my code
نوع المستخدم
مدير
<input class="margin-radio" type="radio" value="clerk" name="usertype"> مساعد مدير
<input class="margin-radio" type="radio" value="client" name="usertype"> عميل
</div>
<!-- ------------- -->
<div class="all-input">
<label for="">الاسم الاول:</label>
<input type="text" name="firstname" value="<?php if(isset($_POST['firstname'])) echo $_POST['firstname'];?>" required>
</div>
<!-- ------------ -->
<div class="all-input">
<label for="">الاسم الأخير:</label>
<input type="text" name="lastname" value="<?php if(isset($_POST['lastname'])) echo $_POST['lastname'];?>" required>
</div>
<!-- ------ -->
<div class="all-input">
<label for="">البريد الالكتروني:</label>
<input type="email" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" required>
You can use the Checked attribute of the <input type="radio"
For example:
<input type="radio" name="foo" value="bar" <?php if(isset($_POST['foo']) && $_POST['foo'] == 'bar') echo 'checked="checked"'; ?>>
This method also works for the type="checkbox" In that case you would need to iterate over the array of possible values.
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 wondering how I can extract the title variable from my forms posted fields array.
I know $_POST['name'] is the field name but can I do something like $_POST[title] to get the title?
I ask because I have a dynamic form with variable lengths. The dynamics is group_one contains 5 fields, group_two contains 12 fields, group_three contains 2 fields for example.
I am hoping to loop through these groups and post the title of the form field to a column in a DB and its value. Any help appreciated with understanding if I can use the 'title' variable in the form field element.
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesYes" value="Yes" title="Dog at premises" />Yes
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesNo" value="No" title="Dog at premises" />No
<input type="text" name="txtGroupOne[]" id="txtNextOfKinName" title="Next of kin name" />
<input type="text" name="txtGroupOne[]" id="txtNextOfKinContact" title="Next of kin contact" />
No, only the name="x" attribute is sent by default in an HTML Form. title="x" is not sent. You can technically get around it with some crazy Javascript and an Ajax POST, but I would avoid that if I were you.
What do you want the title sent to your server side for? There surely probably a better alternative to achieve your goal.
You can add hidden field to each of fields that need titles to be sent to server besides their values.
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesYes" value="Yes" />
<input type="hidden" name="txtGroupOneTitles[]" value="Dog at premises" >
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesNo" value="No" />
<input type="hidden" name="txtGroupOneTitles[]" value="Dog at premises" >
<input type="text" name="txtGroupOne[]" id="txtNextOfKinName" />
<input type="hidden" name="txtGroupOneTitles[]" value="Next of kin name" >
<input type="text" name="txtGroupOne[]" id="txtNextOfKinContact" />
<input type="hidden" name="txtGroupOneTitles[]" value="Next of kin contact" >
You can do something like this:
foreach($_POST as $form_var){
// Compile your code here, etc.
}
Anything that does not have a "name" attribute in your form will not be part of the $_POST array.
ie,
<input type='text' id='bar'>
<input type='text' name='foo' id='foo'>
Only $_POST['foo'] would exist.
Misread the question slightly, but you could also prepend your input names to include the "group title" upon generation.
<input type='text' name='title1_foo' id='title1_foo'>
Then you can manipulate the information you want from the $_POST loop.
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.
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"