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.
Related
I have a form as follow.
<form action="action.php" method="post">
<div class="add_another">
<label for="BrotherAdmissionNumber" class="p-label-required">Admission Number</label>
<input type="text" name="BrotherAdmissionNumber[]" placeholder="Admission Number" />
<label for="varName" class="p-label-required">Name</label>
<input type="text" name="BrotherName[]" placeholder="Name" class="form-control" />
<label for="BrotherGrade" class="p-label-required">Grade</label>
<input type="text" id="varGrade" name="BrotherGrade[]" placeholder="Grade" class="form-control" />
<label for="BrotherClassTr" class="p-label-required">Class Teacher</label>
<input type="text" id="varClassTeacher" name="BrotherClassTr[]" placeholder="Class Teacher" class="form-control" />
<button class="btn add_field_button" style="float: right;">Add Another Brother</button>
</div>
<button class="submit" >Submit</button>
</form>
And I am using following jQuery to add another section to the form.
This will create the set of fields again.
<script>
var max_fields = 10;
var wrapper = jQuery(".add_another");
var add_sec_3 = '<div class="add_another" style="border-top: 1px solid #f0f0f0; border-spacing: 7px;"><label for="BrotherAdmissionNumber" class="p-label-required">Admission Number</label><input type="text" name="BrotherAdmissionNumber[]" placeholder="Admission Number" /><label for="varName" class="p-label-required">Name</label><input type="text" name="BrotherName[]" placeholder="Name" class="form-control" /><label for="BrotherGrade" class="p-label-required">Grade</label><input type="text" id="varGrade" name="BrotherGrade[]" placeholder="Grade" class="form-control" /><label for="BrotherClassTr" class="p-label-required">Class Teacher</label><input type="text" id="varClassTeacher" name="BrotherClassTr[]" placeholder="Class Teacher" class="form-control" />Remove</div>';
jQuery(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
jQuery(wrapper).append(add_sec_3); //add section
jQuery('.add_another .add_another:last').hide();
jQuery('.add_another .add_another:last').show('fast');
}
});
</script>
When clicking on add another button a section is added successfully.
So that's fine.
My issue is, I tried several methods of retrieving for arrays but nothing works.
I want to create another form with hidden fields of brother details to send it to another page.
How can I show the all generated fields values in a php page during the submission?
How do I have to update this action.php file?
action.php
<?php
$BrotherAdmissionNumber = $_POST['BrotherAdmissionNumber'];
$BrotherName = $_POST['BrotherName'];
$BrotherGrade = $_POST['BrotherGrade'];
$BrotherClassTr = $_POST['BrotherClassTr'];
foreach(){
//brothers details start here
?>
<form action="action2.php" method="post">
Admission Number :<?= $BrotherAdmissionNumber ?>
<input type="hidden" value="<?= $BrotherAdmissionNumber ?>" name="BrotherAdmissionNumber" />
Name :<?= $BrotherName ?>
<input type="hidden" value="<?= $BrotherName ?>" name="BrotherName" />
Grade :<?= $BrotherGrade ?>
<input type="hidden" value="<?= $BrotherGrade ?>" name="BrotherGrade" />
Teacher :<?= $BrotherClassTr ?>
<input type="hidden" value="<?= $BrotherClassTr ?>" name="BrotherClassTr"/>
<button name="send_to_other_page">CONFIRM</button> <!-- when this button is clicked the form shoukd send the hidden values -->
</form>
<?php } ?>
Now when send_to_other_page button is clicked the form should send the hidden values to next page action2.php
What I have to update in foreach loop above?
How I can collect these details in action2.php ?
Take a look at this link
You have to serialise form data and then you can easily retrieve and save in database.
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);
}
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>
Here is the code snippet, which is inside a form:
<fieldset>
<!-- for each radio button, PHP code used to check IF $id==value
to determine what should be checked. -->
<div class="newrow">
<label>Status:</label>
<div class="radio-group">
<input type="radio" id="active" value="active" <?php echo ($status_id==1)?'checked':'' ?> name="status">
<label for="active">Active</label>
<input type="radio" id="on-leave" value="leave" <?php echo ($status_id==2)?'checked':'' ?>name="status">
<label for="on-leave">On Leave</label>
<input type="radio" id="terminated" value="terminated" <?php echo ($status_id==3)?'checked':'' ?>name="status">
<label for="terminated">Terminated</label>
</div>
</div>
<div class="newrow">
<label>Eligible for rehire?</label>
<div class="radio-group">
<input type="radio" id="yes" value="yes" <?php echo ($rehire_id==1)?'checked':'' ?>name="rehire">
<label for="yes" class="radio_label">Yes</label>
<input type="radio" id="no" value="no" <?php echo ($rehire_id==2)?'checked':'' ?> name="rehire">
<label for="no" class="radio_label">No</label>
</div>
</div>
</fieldset>
The first thing I want to point out is that in the first row, I decide what to check based on $status_id. For this row if $status_id == 1, the first radio button is checked. BUT if it equals 2 or 3, nothing gets checked.
But in the second row, I decide what to check based on $rehire_id. If $rehire == 2, the second button is checked, BUT NOTHING happens if it equals 1.
WHAT is going on here? Any ideas?
You have no space between checked and the name tag in the cases where it's not working.
When $status_id==2 is true
value="leave" <?php echo ($status_id==2)?'checked':'' ?>name="status">
will output
value="leave" checkedname="status">
instead of what you expected
value="leave" checked name="status">
Add a space before each name=
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.