Check the radio button based on the value fetched from mysql database - php

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>

Related

Dynamic radio button values change by clicking another radio button

I have same div multiple times.If I click on 'Mr' radio button in first div field should change last div radio button value as 'male' Here my code.It is changing all div values as 'male'.How do I change related form Gender radio button value when clicking the title radio button.Please anybody help me.
<?php
for($i=1;$i<=$totalchilds;$i++){
?>
<div class="primary Applicant-details">
<h3>Tourist Details </h3>
<div>
<label>Title : </label>
<input type="radio" class="gender" name="c_genderinfo_<?=$i?>" value="Mr" checked>
Mr
<input type="radio" class="gender1" name="c_genderinfo_<?=$i?>" value="Mrs">
Mrs </div>
<div>
<label for="fname">Name</label>
<input type="text" name="c_name_<?= $i ?>" placeholder="child name">
</div>
<div>
<label for="lname">Age</label>
<input type="text" name="c_age_<?=$i?>" placeholder="Enter child age">
</div>
<div>
<label>Gender : </label>
<input type="radio" class="gender" name="c_gender_<?=$i?>" value="M" checked>
Male
<input type="radio" class="gender1" name="c_gender_<?=$i?>" value="F">
Female </div>
</div>
<?php }?>
To change the gender radio button in the same div use this code:
$('.gender').click(function(){
$(this).closest('.Applicant-details').find('.gender').prop('checked',true);
});
$('.gender1').click(function(){
$(this).closest('.Applicant-details').find('.gender1').prop('checked',true);
});
The structure of the code isn't very optimal. Rename the title from gender to title. Naming the radio buttons gender and gender1 isn't any better. Besides, from a usability perspective, having both selections that have the same value doesn't make a lot of sense.
You can use jQuery. There are different methods to achieve this. This is one of them.
Pass your id and value from html like this:
<div>
<label>Title : </label>
<input onclick="changeGender(<?=$i?>, 'M')" type="radio" class="gender" name="c_genderinfo_<?=$i?>" value="Mr" checked>
Mr
<input onclick="changeGender(<?=$i?>, 'F')" type="radio" class="gender1" name="c_genderinfo_<?=$i?>" value="Mrs">
Mrs
</div>
And in script:
function changeGender(id, value)
{
$("input[name=c_gender_"+id+"][value=" + value + "]").prop('checked', true);
}

How to associate Radio Button value with Input Field

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>

Issues with 'checked' attribute in 'input' form tags within PHP while loops

I'm using a html form to edit sql db, thus I would want to have the form display the value currently in the database. I'm Using a while loop to display all the sql rows. I simple radio button allowing user to chose between 'Listings' (shows as '0' in sql) or 'Recent Transactions" (shown as '1' in sql).
The radio buttons are not pre-filling the value from sqlas checked or not
<form name="edit listing" action="edit_list.php" method="post" id="edit_form" >
<ul>
<?php while ($data=mysqli_fetch_assoc($result)):
$transaction = $data['transaction'];
$chkvalue='checked="checked"'; ?>
<li>
<fieldset>
<legend>Designation <h6>Required</h6></legend>
<input name="transaction" type="radio" tabindex="11" value="0" <?php if ($transaction == '0') echo $chkvalue; ?> />
<label for="listings">Listings</label>
<input name="transaction" type="radio" tabindex="12" value="1" <?php if ($transaction == '1') echo $chkvalue; ?> />
<label for="recent_transactions">Recent Transactions</label>
</fieldset>
<input type="submit" formaction="edit_list.php" value="Submit Changes" />
</form>
</li>
<?php endwhile;mysqli_close($con);?>
</ul>
The short php insert is working, a little. My source code is looking something like this:
<input name="transaction" type="radio" tabindex="11" value="0" />
<label for="listings">Listings</label>
<input name="transaction" type="radio" tabindex="12" value="1" checked="checked" />
<label for="recent_transactions">Recent Transactions</label>
But the radio button is not pre-filling.
I've been poking at this all day, any suggestion here where problem could be would be extremely helpful
You only have to write checked not check="checked"!
So change it to this:
$chkvalue='checked'; ?>
instead of this:
$chkvalue='checked="checked"'; ?>

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.

Selecting multiple radio buttons (through jquery, javasscript?)

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.

Categories